Sometimes we will find situations where the type of a data type needs to be cast.Casting means conversion of one data type to another data type.
In java type casting can be classified in three categories:
Implicit casting
Explicit casting
Boolean casting.
In java type casting can be classified in three categories:
1. Implicit casting:- A data type of lower size is assigned to the data type of higher size.This is also named automatic type conversion.For ex:
int x = 9;
double y = x;
System.out.println(y);
In the above code 4 byte integer value is assigned to 8 byte double value.
2. Explicit casting:- The problem is cast the data type higher size to lower size.So this problem is overcome by the Explicit casting that is not done by the JVM , it is done by the programmer.For ex:
double x = 8.5;
int y = x;
In this code compilation error created because it is Implicit casting rule but it converts higher size Double to lower size Int.So explicit casting is:
double x = 8.5;
int y = (int) x;
In this code the Double x is explicitly converted to Int y.
3. Boolean casting:- The Boolean value cannot be assigned to any other data type.Except Boolean all the other 7 data types are converted implicitly or explicitly.We can also say Boolean is incompatible for conversion.
No comments:
Post a Comment