Java - Type Casting

Java

In this tutorial we will learn about type casting in Java programming language.

What is Type Casting?

Type casting is a process of converting one data type into another.

We may encounter a scenario were we may have to convert data of one type into other type. And to carry out that task we use the following syntax of type casting.

data_type variableName = (data_type) varName;

Where, varName is a variable that we are type casting into given data_type and saving the result in other variable variableName.

In the following example we are type casting variable of type int into type double.

// variable of type int
int a = 10;

// variable of type double
double d;

// type cast int to double
d = (double) a;

Automatic (Implicit) Type Casting

In Java, if destination variable has enough space to accommodate value of the source variable then Java will automatically perform the type casting.

For example a variable of type int is of 4 bytes while a variable of type byte is of 1 byte size. So, the byte variable will be automatically type casted into int.

// byte variable
byte b = 10;

// int variable
int i;

// automatic type casting
i = b;

What is Explicit Type Casting?

In this we explicitly mention the data type that we want to cast into.

In the following example we are type casting variable of type float to int.

// float variable
float f = 10.11F;

// int variable
int i;

// explicit type casting
i = (int) f;

Widening and Narrowing

The process of assigning a smaller data type to a larger data type is known as widening.

The process of assigning a larger data type to a smaller data type is known as narrowing.

Narrowing leads to loss of information.

Automatic type casting in expression

When evaluating an expression the computer takes one operator at a time which involves two operands. If the operands are of different data type then the smaller data type operand is automatically type casted into higher data type to match the other operand.

So, if an expression contains byte, short and int type operands then the result is always converted into type int to avoid overflow.

Similarly, if there is a single long type operand in the expression then the whole expression is promoted to long.

Similarly, if the expression contains a single float type operand then the whole expression is promoted to float.

Similarly, if the expression contains a single double type operand then the whole expression is promoted to double.

Following is the type conversion chart.

The final result depends on the data type of the variable on the left side of the assignment operator sign.

So, if the variable on the left side is of smaller data type then the narrowing of result occurs.

When double is converted into float it causes rounding of digits.

When float is converted into long and int it causes truncation of fractional part.

When long is converted into int it causes loss of excess higher bits.

Explicit type casting in expression

For explicit type casting we use the following syntax.

(type) expression

In the following example we are performing division using two integer numbers.

class Math {
  public static void main(String args[]) {
    int a = 5;
    int b = 2;
    float result = a/b;
    System.out.println("Result: " + result);
  }
}

Output:

Result: 2.0

We can see that even though we are saving the result in a floating point data type variable but still we are not getting the actual answer.

This is because both the operands a and b are of type int so, the decimal part of the result is getting truncated.

To solve this problem we have to type cast any of the variable as shown below.

class Math {
  public static void main(String args[]) {
    int a = 5;
    int b = 2;
    float result = (float) a/b;
    System.out.println("Result: " + result);
  }
}

Output:

Result: 2.5

Now, we get the required answer.