Type Conversion in Java
In Java, type conversion means converting one data type into another.
There are mainly two types of type conversion:
1. Implicit Type Conversion (Widening)
This conversion is done automatically by Java.
👉 Condition:
Destination data type should be wider (larger) than the source data type. So, no data loss happens.
Example:
int a= 12;
double b=a;
another one-
char c= ‘a’;
int b= c; //b=97
Here, char is converted into int
This is also called widening conversion
2. Explicit Type Conversion (Casting)
This conversion is done manually by the programmer.
Get Rishil Aman’s stories in your inbox
Join Medium for free to get updates from this writer.
Used when:
- Destination type is smaller
- There may be data loss
Example:
double c= 0.111;
int a= (int)c;
Here, we cast manually using (int)
3. Truncating Conversion
When converting from:
float → int
double → int
The decimal part is removed (truncated)
Invalid Conversions
Some conversions are not allowed in Java:
Boolean cannot be converted to any other data type.
#java




