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.
→ 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.
Some Example for Type Conversion in Java
1. byte a=10;
because [range of int > byte]
2. int a=10;
because [range of byte<int] ————possible loss of precision
you have to do explicit conversion
3. int a=128;
because —[ range of int>byte]
b= (byte)a; ✔
[b will become -128 as here after type conversion, rotation will take place]
4. short a=10;
because range of [short<int]
5. int a =10;
because range of [int>short]
(you have to do explicit conversion)
6. int a=32768;
But yet value is also out of range java does rotation and assign the value -32768 to the variable b
7. int a=10;
because range wise [long >int] so, its correct.
8. long a=10;
because long value can’t assign to int , it’s an error
you have to do explicit conversion.
9. long a=2147483648; ✗ error: value too large to be represented as integer
because if any integer constant crosses the range of int data type then it has no meaning for java compiler.
because long is assigned to integer so,error: possible loss of precision.
value is out of range so, compiler rotates it’s values(-2147483648)
10. byte a=10;
because range wise char is not > than byte as char starts from 0 and byte starts from -128.
11. char a=‘x’;
because range wise byte is not > than char as byte ends at 127 while char ends at 65535
12. short a=10;
because range wise char is not > than short as char starts from 0 and short starts from -32768.
13. char b=‘x’;
because range wise short is not > than char as short ends at 32767 while char ends at 65535.
14. int a=10;
because range wise char is smaller than int.
15. char b=‘x’;
because range wise int is > than char
16. double a=1.7;
because [double>float] possible loss of precision.
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
18. float a=1.7f;
19. boolean a=true;
20. boolean a=true;
21. int a=1;
22. int a=1;
Error Message :- Incompatible Types
because boolean cannot be converted into any type implicitly or explicitly.