Java Type Casting with Example
Type casting in Java is the process of converting an object of one data type to another. It can be performed either automatically or explicitly, depending on the requirements of the code. Automatic type casting occurs when the data type of the expression on the right-hand side of the assignment operator can be promoted to the data type of the variable on the left-hand side. On the other hand, explicit type casting requires the programmer to specify the target data type for the conversion. It’s important to note that explicit type casting can result in data loss if the value being cast is too large to fit in the target data type.
Java Type Casting
In Java, type casting is the process of converting an object of one data type to another. This can be done in two ways:
- Automatic Type Casting: Automatic type casting occurs when the data type of the expression on the right-hand side of the assignment operator can be automatically promoted to the data type of the variable on the left-hand side.
For example:
int a = 10; long b = a; // automatic type casting from int to long
- Explicit Type Casting: Explicit type casting is when you explicitly specify the data type to which you want to convert an expression.
For example:
double d = 35.5; int i = (int) d; // explicit type casting from double to int
It’s important to note that explicit type casting can potentially result in data loss if the value of the expression being cast is too large to fit in the target data type.
Type Of Java Type Casting
In Java, there are two types of type casting:
Automatic Type Casting (Widening Casting)
Automatic type casting, also known as “implicit type casting,” occurs when the data type of the expression on the right-hand side of the assignment operator can be automatically promoted to the data type of the variable on the left-hand side. This is possible when there is a relationship of inheritance or compatibility between the two data types. For example:
int a = 10; long b = a; // automatic type casting from int to long
Here, the value of the int
variable a
is being assigned to the long
variable b
. Since long
is a larger data type than int
, the value of a
can be easily stored in b
without any loss of precision.
Explicit Type Casting (Narrowing Casting)
- Explicit type casting, also known as “type casting,” is when you explicitly specify the data type to which you want to convert an expression. This is necessary when there is no relationship of inheritance or compatibility between the two data types being converted.
For example:
double d = 35.5; int i = (int) d; // explicit type casting from double to int
Here, the value of the double
variable d
is being cast to an int
data type. Since int
is a smaller data type than double
, some information will be lost in the conversion process.
It’s important to note that explicit type casting can potentially result in data loss if the value of the expression being cast is too large to fit in the target data type. Therefore, it’s important to be careful when using explicit type casting and make sure that the data being converted will fit in the target data type.