Search Logic Blocks

Friday, July 10, 2020

Java: Type Casting

Sometimes we need to assign one data type value to another data type variable - like int value to a float variable. This is called Type Conversion or Type Casting. Java supports strict type checking. So, type conversion depends on the compatibility of respective data types. Type Conversion are done in two ways -
  • Implicit Type Conversion
  • Explicit Type Conversion

Implicit Type Conversion
When the two data types are compatible to each other, the assignment operation converts automatically the right side value to the data type of the left side. This is called Implicit Type Conversion. Automatic type conversion (implicit type conversion) mostly happens only when the left side data type is larger than the right side data type. So, this kind of type conversion is also called widening conversion. Referring my earlier post Data Typesbyte, short and char data types are converted to int data type; int data type converts to long data type; int and long data types convert to float data type; float data type converts to double data type.

Class ImplicitTypeConvert (ImplicitTypeConvert.java)
public class ImplicitTypeConvert {
public static void main(String args[]) {
byte b = 100;
//byte b2 = b * 3;
int i = b * 3;
System.out.println("b: " + b);
System.out.println("i: " + i);

int i2 = i / 7;
double d1 = i / 7;

System.out.println("i2: " + i2);
System.out.println("d1: " + d1);

double d2 = i / 7.0;
System.out.println("d2: " + d2);
}
}
In the above example, we are assigning byte data type value to int data type variable, and then int data type value to float data type variable. 

When we use the byte value in the following statements, first byte value is converted automatically to int data type and then multiplies by 3, and the result is assigned to int data type variable. But reverse is not allowed, as the result int data type value can't be assigned to a byte data type variable.
byte b = 100;
//byte b2 = b * 3;
int i = b * 3;
System.out.println("b: " + b);
System.out.println("i: " + i);
It's output will be like - 

b: 100
i: 300

In the second part, i is an int data type variable, so when you divide int data type value by an integer number, the result will be an int data type value, assigning result to the double data type variable will automatically convert the result value into the double data type.
int i2 = i / 7;
double d1 = i / 7;

System.out.println("i2: " + i2);
System.out.println("d1: " + d1);
It's output will be like -

i2: 42
d1: 42.0

But in next situation, int data type variable is divided by  7.0, which is a double value. So, first the int data type variable value is converted into double data type variable value and then divided by 7.0. The result will be automatically double value, which is assigned to the double data type variable, means no type conversion is required. 
double d2 = i / 7.0;
System.out.println("d2: " + d2);
And the output will be like -

d2: 42.857142857142854
 
Explicit Type Conversion
Sometimes, when two data types are not compatible, and if we have to assign a value to smaller data type variable or incompatible data type variable, we need to convert data types explicitly. So, this kind of type conversion is also called narrowing conversion. We already have used the Explicit Type Conversion for assigning int value to char data type in previous posts - Char Data Type and Getting Char Input From Console

Class ExplicitTypeConvert (ExplicitTypeConvert.java)
public class ExplicitTypeConvert {
public static void main(String args[]) {
byte b = 100;
//byte b2 = b * 3;
int i = b * 3;
byte b2 = (byte)(b * 3);
System.out.println("b: " + b);
System.out.println("i: " + i);
System.out.println("b2: " + b2); // data loss

int i2 = i / 7;
double d1 = i / 7;

System.out.println("i2: " + i2);
System.out.println("d1: " + d1);

double d2 = i / 7.0;
double d3 = (double)i / 7;

System.out.println("d2: " + d2);
System.out.println("d3: " + d3);
}
}
I have modified my implicit conversion example above a little bit to show the differences between the two types of conversions. General form of explicit conversion is -

target variable = (target-type) expression

In the following statement, the result value of the expression is int data type, but it is casted into byte data type variable by using the (byte) keyword. 
byte b2 = (byte)(b * 3);
As we learned in data types, byte data type has 8 bits and int data type has 32 bits. While casting the large data type value to short data type variable, the uppermost bits get truncated and will be loss of data. It is obvious in the output.
byte b = 100;
//byte b2 = b * 3;
int i = b * 3;
byte b2 = (byte)(b * 3);
System.out.println("b: " + b);
System.out.println("i: " + i);
System.out.println("b2: " + b2); // data loss
And the output is :

b: 100
i: 300
b2: 44

In the following statement, the int data type variable is casted explicitly to the double data type and then it is divided by whole number 7. 
double d3 = (double)i / 7;
In any expression, all variables and values are converted to the larger data type available in the expression. So, the result is also of the larger data type, in this case it is double data type.
double d2 = i / 7.0;
double d3 = (double)i / 7;

System.out.println("d2: " + d2);
System.out.println("d3: " + d3);
And the output is :

d2: 42.857142857142854
d3: 42.857142857142854

No comments: