Write the process of conversion between objects and basic data types.
Answers
The process of conversion between objects and basic data types is known as type casting or type conversion.
There are mainly two types of types of type casting available in java programming language-
•Implicit type casting/ Implicit type conversion
This is the default type conversion which is automatically done by the java compiler. Whenever the programmer assigns a value of type that is compatible and is within limit of the data type of the variable which it's being assigned to, then the compiler automatically converts that value implicitly. Example:
int x=5;
double y=x;//Now, here, the value of x (i.e. 5) will automatically be converted to double type(i.e. 5.0).
•Explicit type casting/ Explicit type conversion
The type conversion/casting that has to be done manually by the programmer by using the typecast/cast operator. Now, the syntax followed differs between conversion of objects and conversion of variable of basic data types.
For basic data types, the syntax is: data_type1 var1=(data_type1) var2;
where var1 is a variable of data_type1 but var2 isn't a variable of same type.
For conversion between objects, the syntax is: class1 obj1= (class1) obj2;
where obj1 is the object of class1 and obj2 is the object of some other class.