Blog

Type Conversion in Java

Java / Java Tutorials

Type Conversion in Java

What is type conversion ?

Whenever the compiler encounters a statements where the value on right side of assignment is different than the variable on left , then the compiler tries to convert R.H.S to L.H.S and this automatic conversion done by compiler is called as Type Conversion.
For example, consider the following statement:
x = y;
In this case two things might happen:-
1 . If data type of both variable are same then value of y will be assigned to the variable x.
2. But if data type of both variable are different then the value of y needs to be converted as per the data type of variable x and this is called “Type Conversion”

Forms Of Type Conversion

In Java ,type conversion is of two types
(i) Implicit Conversion ( automatically done by compiler)
(ii) Explicit Conversion ( specially done by programmer , also called Type Casting)

Rules For Implicit Conversion

For implicit conversion there are 2 conditions which must be true :
→ The values must be compatible/convertible.
AND
→ The value on RHS of assignment must be smaller than variable on LHS
If both these rules are followed then java will implicitly convert the value otherwise conversion has to be done by the programmer
Let us understand them in depth

Rule 1 : Convertible

→ Convertible means it must be possible for java to convert a value from one form to another.
→ For example , it is possible for java to convert a character to an integer using it’s UNICODE/ASCII value . So the following will compile:
int x=‘A’;   
→ But it is not possible for java to convert the boolean value “true” to integer as the values “true” and “false” have no other representation. So the following statement will not compile:
int x=true;

Rule 2 : Smaller

→ Smaller means the range of a variable’s data type must be a smaller than other variable’s range. NOT THE SIZE
→ For example , a short data type variable has a range of -32768 to 32767 which is smaller( proper subset) of the range of an int variable whose range is -2147483648 to 2147483647, so “short” is considered to be smaller than an “int”.
→ Another example, an int is of 4 bytes and has a range of -2147483648 to 2147483647 while a float is also of 4 bytes but has a range of -3.4*10^38 to 3.4 *10^38 which is greater than the range of int , so int is smaller than float.
[vc_row][vc_column][vc_column_text]

Some Example for Type Conversion in Java

[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/2″][vc_column_text]1. byte a=10;
int b;
b=a;
     
because [range of int > byte][/vc_column_text][/vc_column][vc_column width=”1/2″][vc_column_text]2. int a=10;
byte b;
b=a;

because [range of byte<int] ————possible loss of precision
b=(byte)a;
you have to do explicit conversion[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/2″][vc_column_text]3. int a=128;
byte b;
b=a;
   
because —[ range of int>byte]
b= (byte)a;
[b will become -128 as here after type conversion, rotation will take place][/vc_column_text][/vc_column][vc_column width=”1/2″][vc_column_text]4. short a=10;
int b;
b=a;

because range of [short<int][/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/2″][vc_column_text]5. int a =10;
short b;
b=a;

because range of [int>short]
b=(short)a;
(you have to do explicit conversion)[/vc_column_text][/vc_column][vc_column width=”1/2″][vc_column_text]6. int a=32768;
short b;
b=a;

b=(short)a;  
But yet value is also out of range java does rotation and assign the value -32768 to the variable b[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/2″][vc_column_text]7. int a=10;
long b;
b=a;

because range wise [long >int] so, its correct.[/vc_column_text][/vc_column][vc_column width=”1/2″][vc_column_text]8. long a=10;
int b;
b=a;

because long value can’t assign to int , it’s an error
b=(int)a;
you have to do explicit conversion.[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/2″][vc_column_text]9. long a=2147483648; error: value too large to be represented as integer
int b;
b=a;
because if any integer constant crosses the range of int data type then it has no meaning for java compiler.
long a=2147483648L;
int b;
b=a;
because long is assigned to integer so,error: possible loss of precision.
b=(int)a;
value is out of range so, compiler rotates it’s values(-2147483648)[/vc_column_text][/vc_column][vc_column width=”1/2″][vc_column_text]10. byte a=10;
char b;
b=a;

because range wise char is not > than byte as char starts from 0 and byte starts from -128.[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/2″][vc_column_text]11. char a=‘x’;
byte b;
b=a;

because range wise byte is not > than char as byte ends at 127 while char ends at 65535[/vc_column_text][/vc_column][vc_column width=”1/2″][vc_column_text]12. short a=10;
char b;
b=a;

because range wise char is not > than short as char starts from 0 and short starts from -32768.[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/2″][vc_column_text]13. char b=‘x’;
short a;
a=b;

because range wise short is not > than char as short ends at 32767 while char ends at 65535.[/vc_column_text][/vc_column][vc_column width=”1/2″][vc_column_text]14. int a=10;
char b;
b=a;

because range wise char is smaller than int.[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/2″][vc_column_text]15. char b=‘x’;
int a;
a=b;

because range wise int is > than char[/vc_column_text][/vc_column][vc_column width=”1/2″][vc_column_text]16. double a=1.7;
float b;
b=a;

because [double>float] possible loss of precision.
b=(float)a;  [/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/2″][vc_column_text]17. float a=1.7;
because every decimal constant by default is a double and double is > than float. Solution is to suffix it with f or F[/vc_column_text][/vc_column][vc_column width=”1/2″][vc_column_text]18. float a=1.7f;
double b;
b=a;
[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/2″][vc_column_text]19. boolean a=true;
int b;
b=a;
[/vc_column_text][/vc_column][vc_column width=”1/2″][vc_column_text]20. boolean a=true;
int b;
b=a;
[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/2″][vc_column_text]21. int a=1;
boolean b;
b=(boolean)a;
[/vc_column_text][/vc_column][vc_column width=”1/2″][vc_column_text]22. int a=1;
boolean b;
b=(boolean)a;

Error Message :- Incompatible Types
because boolean cannot be converted into any type implicitly or explicitly.[/vc_column_text][/vc_column][/vc_row]