In Java, data needs to have a type. Sometimes, we need to change the type of the same data. When we change type, it’s called Type Conversion. When we do type conversion, it has some impact, and we need to write our program keeping all those things in mind. For that, we need to understand how type conversion works.
From Java Data Types, we have learnt, variable declaration defines type as well as the size of data it’s going to hold. For example, a byte can hold -128 to 127, and a short can hold -32768 to 32767. Now, if we want to convert from byte to short or short to byte, how would that work?
What are the different Type Conversions in Java?
Implicit Conversion :
There are certain data conversions that the Java compiler can handle automatically. This type of conversion happens mostly from smaller to larger data types. No data loss happens for Implicit conversion. For Primitive data types, below is the order for implicit conversion
byte -> short -> car -> int -> long -> float -> double
byte b = 100;
short s = b;
System.out.println("After Conversion from byte to short : " + s);
int i = 123456;
long l = i;
System.out.println("After Conversion from into to long : " + l);
float f = 4657.69f;
double d = f;
System.out.println("After Conversion from float to double : " + d);
Implicit Type Conversion in Java
---------------------------
After Conversion from byte to short : 100
After Conversion from into to long : 123456
After Conversion from float to double : 4657.68994140625
As you can see, we didn’t do anything other than assigning the variable to a new data type. It worked fine without any complaint. Let’s see why we need explicit conversion.
Explicit Conversion :
Size is critical for data type conversion. Like I was saying, a byte can hold -128 to 127, and a short can hold -32768 to 32767. Now, if we want to convert a short variable to a byte type, the compiler will not allow that automatically. Because there is a chance of data loss. If we assign 567 to a short variable, a byte variable can’t hold that. Hence, we have to explicitly(manually) convert the data type.
For analogy, we can have Coffee in small, medium, and large cups. If we pour coffee from small to medium/large or medium to large, there is no chance for overflow. But opposite, large to medium/small or medium to small will cause overflow. Also, if we plan to put Coffee on a plate, then that might also cause overflow. So when there is a chance of overflow, we have to take the risk. Same way, when there is a chance of data loss, the compiler will not automatically allow the conversion. We have to manually perform the conversion, taking the risk. This is also called Type Casting.

Like implicit conversion example, I tried the same thing but compiler didn’t allow me and rather showing me compilation error. If you see the error, it’s saying “lossy conversion”, that already I tried to explain.
short s = 1000;
byte b = s;
System.out.println("After Conversion from short to byte : " + b);
error: incompatible types: possible lossy conversion from short to byte
byte b = s;
^
1 error
error: compilation failed
To resolve this issue, we need to take the risk of data loss and manually convert the type using cast operator () =>
Destination_Type var_dest_type = (Destination_Type) var_src_type ;
short s = 1000;
byte b = (byte)s;
System.out.println("After Conversion from short to byte : " + b);
long l = 837366647;
int i = (int)l;
System.out.println("After Conversion from long to int : " + i);
double d = 78459585.6776;
float f = (float)d;
System.out.println("After Conversion from double to float : " + f);
int x = (int)d;
System.out.println("After Conversion from double to int : " + x);
Explicit Type Conversion in Java
---------------------------
After Conversion from short to byte : -24
After Conversion from long to int : 837366647
After Conversion from double to float : 7.845958E7
After Conversion from double to int : 78459585
if you see the examples, you can see the data loss for short to byte and double to int conversion. There is one more thing that we need to discuss, i.e. Type Promotion in Java
What is Type Promotion in Java?
For different data types, when we perform any operation, there might be chance that the result might not be in the range of same data type. For example, 20 is a byte value, but 20 x 20 =400 not in range of byte value. If we try to assign the result in byte variable, there is a chance of data loss. To resolve this issue, Java compiler is smart enough to handle the situation and decides the type of result. For example, byte x byte, Java will expect the result to be int, not byte.
byte b = 20;
int i = b * b;
System.out.println("After Promotion : " + i);
System.out.println("Data Type of result : " + ((Object) i).getClass().getName());
Type Promotion in Java
---------------------------
After Promotion : 400
Data Type of result : java.lang.Integer
Hope this helps to understand Java type conversion.