All the variables declared in Java are classified into certain type. The classification of variables will help us in treating the variables according to its type. Variables in Java are classified as characters, several types of integers, floating points and types to declare a statement true or false. All the above mentioned variable types are called as primitive types.
Class Type Variables in Java
Class type variables are used to solve the object oriented problems. Separate types of variables are used to solve the object oriented programs because they are more complex than simple integer and boolean functions. The variable which names a certain object in a program is classified into class type variables.
- Read: More Interview Questions
Difference between Class and Primitive types:
- Every variable declared in a program is stored in the memory of the computer. The Storing method of class type variables is different from primitive type. The class type variable stores only the memory address of the variable but not its value. If a variable is declared in primitive it stores the value of the variable in the particular memory location.
- The primitive data type always has a value and never be declared as null. If no value is assigned for a primitive data type, a default value of zero will be initialized.
- When a value is assigned to a primitive data type, it will be copied but only the handle is copied in class data type. The class data type will share the variables so change in one variable will affect the other.
- When the equality (==) operator is used in primitive data types only its primitive values are compared. If equality operator is used in class data type, the address which stored the data is compared. Ex: imagine that we have two int variables namely a and b whose value is 10. If a is compared with b using equality operator, the result will declared that the two integers are equal. If two strings namely “USA” and “USA” are compared using “equality” operator the result will state that the strings are not same.
- When primitive values are passed into a method, its values are copied. When class values are passed into method only the handle of class types are copied.
- Primitive type returns its value while only the handle is returned in the case of class type.
- Primitive types are stored in stack while class type are stored as heap which is managed by garbage collector.
- The memory space consumed by primitive data types is very less when compared to the memory type of class type. Class type consumes more memory because it maintains metadata objects.
- Primitive data types can be copied but class data types can be shared.
- All primitive data types have an default value but uninitialized class data types will throw NullPointerException.
Converting Primitive Types into Class Types – Vice Versa
Casting is a process of converting a data type into another. All primitive and class types can be converted into another using the casting process. In Java, the java.lang package is comprised of classes which correspond to all primitive data types. The names of classes present in java.lang package are almost similar to the names of data types. Utilizing the classes in java.lang package, it is possible to create a class type which can create two objects with same value.
Converting a string into a primitive type is a common translation used in Java programming. The primitive data type created by converting a string can be used anywhere in the program without altering its value. Consider the following code as an example:
int b= Integer.parseInt(Bollywood);
In the example code given above, the class Integer is used to convert the string “Bollywood” into “1001”. Similarly classes like Boolean, Float, Double, Void, Short and Long are also used.
Autoboxing and unboxing
Autoboxing and unboxing are the process used to convert the one variable type into another automatically.
Autoboxing is used to convert the primitive type into class type while unboxing is used to convert class type into primitive type. If a statement is written in primitive or class type, it is converted vice versa according to the requirement of the compiler. Conversion using Autoboxing and unboxing works only if the type has a value. If objects which are called using constructors are converted, it will result in a compiler error.