Java Type Casting (AKA Type Conversion)

In this section, we will learn what the type casting is and how it works in Java.

Types of Casting in Java

Casting is about storing a value of one type into a variable of different type.

For example, you might have a float value like 23.454 and now want to store this value in a variable of type integer!

By default, this might end up data corruption and error in the program if we don’t pay careful attention.

But after learning to cast, you can easily run such an operation without worrying about the side effects.

In short, there are two types of casting exist in Java:

  • Implicit casting (AKA Widening Type Casting)
  • Explicit casting (AKA Narrowing Type Casting)

Java Widening Type casting (AKA Automatic Type Casting)

Let’s say we have an integer value 10. Obviously, this value can be stored in a variable of type `int`. But in Java we’re allowed to store this value into a variable of type `double` or `float` as well!

For this particular case all we need to do is to assign this value into a variable of type `double`.

This is called widening casting or indirect casting.

Widening casting happens automatically by the computer, and there’s no work on developers.

The case where widening casting happens is:

When we want to store the value of a variable with lower memory space to a variable of higher memory space.

byte -> short -> char -> int -> long -> float -> double

As the list above shows a byte-type value takes less memory space compared to short-type so a byte value can be stored in a short variable. Or a value of type `int` can be stored in a variable of type `long` again because it takes less memory space etc. and no-compile time error will occur.

Java Widening Type casting Example:

int i = 10;

double d = i;

Behind the scene the compiler will allocate a memory space for the variable `d` and stores the value 10 as the type double in this variable.

Java Narrowing Type Casting (AKA Explicit Type Casting)

This type of casting happens when a value of a data type with higher memory space is about to be stored in a variable with lower memory space.

double -> float -> long -> int -> char -> short -> byte

In such cases, there’s a chance that part of the data will be lost.

By default, the compiler won’t allow to narrow casting unless we explicitly tell the compiler to do so.

This is the way we run a narrow casting:

double d = 2.33;

int i = (int) d;

In this example, we want to store the value of the `d` variable which is of type `double` into the `i` variable which is of type integer.

You see, an integer variable can’t store a value that has fraction. Basically the compiler will return error if we attempt to store a value with fraction point to a variable of type integer.

The reason that compilers return error is that, the fraction part of the value will be lost if it’s going to be stored in a variable of type integer (Hence the value will be narrowed).

But with the help of `narrowing casting` we can signal the compiler that we are aware of the danger of data lost but we still insisting on doing so.

As the example above shows, in order to run a narrow casting, we place the target data type in parentheses in front of the value that we want to cast it.

Java Narrowing Type Casting Example:

public class Simple {
    public static void main(String[] args) {
        double height = 5.6; 
        int i = (int) height;
        System.out.println(i);
    }
}

Output: 5

In this example, if we didn’t use the narrowing casting, the compiler would return error reminding us of the danger of losing data.

Let’s see this in action:

public class Simple {
    public static void main(String[] args) {
        double height = 5.6;
        int i = height;
        System.out.println(i);
    }
}

Output:

Error:(6, 17) java: incompatible types: possible lossy conversion from double to int

 

More to Read:

Java upcasting and downcasting

Java instanceof operator

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies