Java Type Casting

Type casting in Java means converting a value from one data type to another. Casting is useful when a calculation or assignment needs a compatible type.

Some conversions happen automatically because they are safe. Other conversions must be written explicitly because they may lose information.

Widening Casting

Widening casting converts a smaller or less precise type into a larger or more precise type. Java can do this automatically when there is no risk of the value being too large for the new type.

Common widening conversions include int to long, int to double, and float to double.

public class Main {
    public static void main(String[] args) {
        int wholeNumber = 25;
        double decimalNumber = wholeNumber;

        System.out.println("int value: " + wholeNumber);
        System.out.println("double value: " + decimalNumber);
    }
}

Output:

int value: 25
double value: 25.0

How It Works

The variable wholeNumber has the type int. The assignment double decimalNumber = wholeNumber; stores that integer value in a double variable. Java adds the decimal part automatically, so the printed value is 25.0.

This is allowed without any special syntax because every normal int value can fit inside a double.

Narrowing Casting

Narrowing casting converts a larger or more precise type into a smaller or less precise type. Java requires an explicit cast because the result may lose data.

To write an explicit cast, place the target type in parentheses before the value:

public class Main {
    public static void main(String[] args) {
        double price = 19.99;
        int dollars = (int) price;

        System.out.println("price: " + price);
        System.out.println("dollars: " + dollars);
    }
}

Output:

price: 19.99
dollars: 19

The cast (int) price tells Java to convert the double value to an int. The decimal part is removed, so 19.99 becomes 19. It is not rounded.

Casting In Calculations

Casting is also helpful when integer division would remove the decimal part. If both operands are integers, Java performs integer division.

public class Main {
    public static void main(String[] args) {
        int points = 7;
        int total = 10;

        double wrongRate = points / total;
        double rightRate = (double) points / total;

        System.out.println("Without cast: " + wrongRate);
        System.out.println("With cast: " + rightRate);
    }
}

Output:

Without cast: 0.0
With cast: 0.7

In points / total, both values are int, so Java calculates 7 / 10 as integer division and gets 0. That result is then stored as 0.0.

In (double) points / total, the cast changes points to a double first. Java then uses decimal division and keeps the fractional result.

Common Casts

Conversion Kind Example
int to double Widening double x = 5;
double to int Narrowing int x = (int) 5.8;
long to int Narrowing int x = (int) bigValue;
int to char Narrowing char letter = (char) 65;

When To Be Careful

  • Narrowing from double to int removes the decimal part.
  • Narrowing from a large number type to a smaller number type can produce an unexpected value if the number is too large.
  • Casting does not change the original variable unless you assign the converted value back to a variable.
  • Casting between unrelated types, such as String and int, is not allowed with a simple cast.

Takeaway: Java widens safe conversions automatically, but narrowing conversions need an explicit cast and should be used carefully.