Type Conversion
There are 8 primitve data types in Java
There are two type of conversion:
1. Widening or automatic type conversion
2. Narrowing or Explicit Conversion
Widening:
Widening conversion takes place when two data types are automatically converted. This
happens when:
● The two data types are compatible.
● When we assign value of a smaller data type to a bigger data type.
When we need to convert from primitive smaller to bigger size one, we don’t need to
use any specific format. We can directly assign the values.
Eg:
int myInt = 0;
long myLong = myInt;
Narrowing or Explicit Conversion:
If we want to assign a value of larger data type to a smaller data type we perform
explicit type casting or narrowing.
● This is useful for incompatible data types where automatic conversion
cannot be done.
● Here, target-type specifies the desired type to convert the specified value to.
Type promotions:While evaluating expressions, the intermediate value may
exceed the range of operands and hence the expression value will be promoted.
Some conditions for type promotion are:
● Java automatically promotes each byte, short, or char operand to int
when evaluating an expression.
● If one operand is a long, float or double the whole expression is promoted
to long, float or double respectively.
Eg:
Explicit type casting in Expressions
While evaluating expressions, the result is automatically updated to larger data type of
the operand. But if we store that result in any smaller data type it generates compile
time error, due to which we need to type cast the result.
Some examples
1.
Public static void printDouble(){
double d= 100.04;
long l = (long)d;
int i = (int)l;
System.out.println("Double value "+d);
//fractional part lost
System.out.println("Long value "+l);
//fractional part lost
System.out.println("Int value "+i);
}
2.
class Test
{
public static void main(String[] args)
{
int i = 100;
//automatic type conversion
long l = i;
//automatic type conversion
float f = l;
System.out.println("Int value "+i);
System.out.println("Long value "+l);
System.out.println("Float value "+f);
}
}
Automatic type conversion is happening when we assign an int value to a
float. Because it is int is shorter than float.
3.
public static void charToInt(){
char ch = 'c';
int num = 88;
ch = num;
System.out.println("char----------"+ch);
}
error: incompatible types: possible lossy conversion from int to
char
ch = num;
If we cast the num to char
Ch = char(num); // The value you are getting is char value
Note: you cannot convert int to char.
4.
public static void byteTodouble(){
byte b;
int i = 257;
double d = 323.142;
System.out.println("Conversion of int to byte.");
//i%256
b = (byte) i;
System.out.println("i = " + i + " b = " + b);
System.out.println("\nConversion of double to byte.");
//d%256
b = (byte) d;
System.out.println("d = " + d + " b= " + b);
}
Output:
Conversion of int to byte.
i = 257 b = 1
Conversion of double to byte.
d = 323.142 b = 67
Note: we can convert int to byte and double to byte.