The process of converting data from one data type to another data type is called Type Casting.
The main Advantage of Type Casting is Flexibility.
There are two types of Type Castings in Java.
1. Primitive Data Types Type casting
2. User Defined Data Types Type Casting
User Defined Data Types Type Casting:
The process of converting data from one User defined data Type to another user defined data type is called User defined Data Types Type Casting.
To perform User defined data Types Type Casting, we need either ‘extends’ relation or ‘implements’ relation between two user defined data types.
Primitive Data Types Type casting:
The process of converting data from one Primitive Data Type to another Primitive Data Type is called Primitive Data Types Type Casting.
There are two types of Primitive Data Types Type casting.
- Implicit Type Casting
- Explicit Type Casting
Implicit Type casting
The process of converting data from lower data Type to higher data type is called Implicit Type casting.
To cover all the possibilities of implicit type casting JAVA has provided the following chart.
1 2 4 8 4 8
byte—->short—-->int—--->long—-->float—--->double
^
|
char
2
In Java applications, to perform implicit Type casting we have to assign lower data type variables to the Higher data type variables.
byte b = 10;
int i = b;// Implicit Type Casting
When we compile the above assignment statement , the compiler will perform the following actions.
- Compiler will recognize both right side variable data type and Left side variable data type.
- Compiler will check whether the right side variable’s data type is compatible with the left side variable’s data type or not.
- If the right side variable’s data type is not compatible with the left side variable’s data type then the compiler will raise an error.
JAVA7: Possible loss of precision.
JAVA8: Incompatible Types: Possible lossy conversion from abc to xyz
- If the right side variable’s data type is compatible with the left side variable’s data type then the compiler will not raise any error and at the same time the compiler will not perform any type casting.
Note: In Java, Compiler is responsible for Type Checking only, it is not responsible for Type Casting.
Note: In Java, lower data types are compatible with higher data types, but Higher data types are not compatible with lower data types , so we can assign lower data types to highest data types, but we are unable to assign higher data types to lower data types.
When we execute the above assignment statement, JVM will perform the following actions.
- JVM will convert right side variable’s data type to left side variable’s data type internally, it is implicit Type casting.
- JVM will copy the data from right side variable to left side variable.
EX: implicit type casting
class Test{
public static void main(String[] args){
byte b = 10;
int i = b;
System.out.println(b+" "+i);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
10 10
EX: copying higher data type to lower data type – ERROR
class Test{
public static void main(String[] args){
int i = 10;
byte b = i;
System.out.println(i+" "+b);
}
}
D:\Fullstack10AM>java7.bat
D:\Fullstack10AM>set path=C:\java\jdk1.7.0_80\bin;
D:\Fullstack10AM>javac Test.java
Test.java:4: error: possible loss of precision
byte b = i;
^
required: byte
found: int
1 error
D:\Fullstack10AM>java8.bat
D:\Fullstack10AM>set path=C:\java\jdk1.8.0_202\bin;
D:\Fullstack10AM>javac Test.java
Test.java:4: error: incompatible types: possible lossy conversion from int to byte
byte b = i;
^
1 error
EX: incompatible data types
class Test{
public static void main(String[] args){
byte b = 65;
char c = b;
System.out.println(b+" "+c);
}
}
D:\Fullstack10AM>javac Test.java
Test.java:4: error: incompatible types: possible lossy conversion from byte to char
char c = b;
^
1 error
EX:
class Test{
public static void main(String[] args){
char c = 'A';
short s = c;
System.out.println(c+" "+s);
}
}
D:\Fullstack10AM>javac Test.java
Test.java:4: error: incompatible types: possible lossy conversion from char to short
short s = c;
^
1 error
Note: In Java, no conversions exist between byte and char, short and char in implicit type casting as per their internal data representations.
EX: implicit type casting between char literal to integer
class Test{
public static void main(String[] args){
char c = 'A';
int i = c;
System.out.println(c+" "+i);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
A 65
EX: incompatible data types
class Test{
public static void main(String[] args){
float f = 22.22f;
long l = f;
System.out.println(f+" "+l);
}
}
D:\Fullstack10AM>javac Test.java
Test.java:4: error: incompatible types: possible lossy conversion from float to long
long l = f;
^
1 error
EX: long to float conversion
class Test{
public static void main(String[] args){
long l = 10;
float f = l;
System.out.println(l+" "+f);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
10 10.0
EX: int to byte conversion – error
class Test{
public static void main(String[] args){
byte b = 128;
System.out.println(b);
}
}
D:\Fullstack10AM>javac Test.java
Test.java:3: error: incompatible types: possible lossy conversion from int to byte
byte b = 128;
^
1 error
Status: If we assign a value to the byte variable which is exceeding the max value of byte then the provided constant value will come under the next higher data type, here the next higher data for the byte and short is int only.
class Test{
public static void main(String[] args){
byte b1 = 60;
byte b2 = 70;
byte b = b1 + b2;
System.out.println(b);
}
}
D:\Fullstack10AM>javac Test.java
Test.java:5: error: incompatible types: possible lossy conversion from int to byte
byte b = b1 + b2;
^
1 error
class Test{
public static void main(String[] args){
byte b1 = 30;
byte b2 = 30;
byte b = b1 + b2;
System.out.println(b);
}
}
D:\Fullstack10AM>javac Test.java
Test.java:5: error: incompatible types: possible lossy conversion from int to byte
byte b = b1 + b2;
^
1 error
Reason: If we add two data type variables then the result of this addition may not be of the same data type, it may be of the next higher data type.
X,Y and Z are three primitive data types,
Z = X + Y
- If X and Y belong to {byte, short, int} then Z should be int only.
- If either X or Y or both X and Y belong to {long, float, double} then Z should be higher(X,Y).
byte+byte=int
byte+short=int
short+int=int
int+int=int
byte+long=long
short+float=float
int+double=double
long+float=float
float+float=float
long+double=double
—--
—--
class Test{
public static void main(String[] args){
float f = 22.22f;
long l = 10;
long l1 = f+l; // ERROR
System.out.println(l1);
}
}
D:\Fullstack10AM>javac Test.java
Test.java:5: error: incompatible types: possible lossy conversion from float to long
long l1 = f+l;
^
1 error
EX:
class Test{
public static void main(String[] args){
long l = 10;
float f = 22.22f;
float f1 = l + f;
System.out.println(f1);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
32.22
Explicit Type Casting
The process of converting data from higher data type to lower data type is called Explicit type casting.
To perform explicit type casting we have to use the following pattern.
P a = (B) b;
Where b is a variable of type higher than P.
Where (Type) is called Cast Operator.
Where B should be either same as P or lower than P.
int i = 10;
byte b = (byte)i;
When we compile the above assignment statement, the compiler will perform the following actions.
- Compiler will recognize the cast operator provided data type and the left side variable data type.
- Compiler will check whether the cast operator provided data type is compatible with the left side variable’s data type or not.
- If the cast operator provided data type is not compatible with the left side variable’s data type then the compiler will raise an error.
- If the cast operator provided data type is compatible with the left side variable’s data type then the compiler will not raise any error, at the same time the compiler will not perform any type casting.
When we execute the above assignment statement , JVM will perform the following actions.
- JVM will convert the right side variable’s data type to the cast operator provided data type at the right side of the assignment statement, it is called Explicit Type Casting or Forcible Type Casting.
- JVM will copy the data from right side variable to left side variable.
Examples
EX:
class Test{
public static void main(String[] args){
int i = 10;
byte b = (byte)i;
System.out.println(i+" "+b);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
10 10
EX:
class Test{
public static void main(String[] args){
float f = 22.22f;
long l = (long)f;
System.out.println(f+" "+l);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
22.22 22
EX:
class Test{
public static void main(String[] args){
float f = 22.22f;
long l = (byte)f;
System.out.println(f+" "+l);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
22.22 22
EX:
class Test{
public static void main(String[] args){
byte b = 65;
char c = (char)b;
System.out.println(b+" "+c);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
65 A
EX:
class Test{
public static void main(String[] args){
char c = 'A';
short s = (short)c;
System.out.println(c+" "+s);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
A 65
EX:
class Test{
public static void main(String[] args){
char c = 'A';
short s = (byte)c;
System.out.println(c+" "+s);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
A 65
EX:
class Test{
public static void main(String[] args){
short s = 65;
char c = (char)s;
System.out.println(s+" "+c);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
65 A
incompatible types —
EX:
class Test{
public static void main(String[] args){
short s = 65;
char c = (byte)s;
System.out.println(s+" "+c);
}
}
D:\Fullstack10AM>javac Test.java
Test.java:4: error: incompatible types: possible lossy conversion from byte to char
char c = (byte)s;
^
1 error
Interesting example —
EX:
class Test{
public static void main(String[] args){
int i = 130;
byte b = (byte)i;
System.out.println(i+" "+b);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
130 -126
class Test{
public static void main(String[] args){
int i = 140;
byte b = (byte)i;
System.out.println(i+" "+b);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
140 -116
EX:
class Test{
public static void main(String[] args){
byte b1 = 60;
byte b2 = 70;
byte b = (byte)b1+b2;
System.out.println(b);
}
}
D:\Fullstack10AM>javac Test.java
Test.java:5: error: incompatible types: possible lossy conversion from int to byte
byte b = (byte)b1+b2;
^
1 error
EX:
class Test{
public static void main(String[] args){
byte b1 = 60;
byte b2 = 70;
byte b = (byte)(b1+b2);
System.out.println(b);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
-126
EX:
class Test{
public static void main(String[] args){
double d = 22.22;
byte b = (byte)(short)(int)(long)(float)d;
System.out.println(d+" "+b);
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
22.22 22