Shiklo Programming
EN
  • English
  • Español
  • Português
  • Deutsch
  • Français
  • 日本語
  • हिन्दी

Java Type Casting

Sometimes a value has one data type and you need another: a whole number in a calculation with decimals, a decimal result that must become a whole number, or a number typed by the user as text. Converting a value from one type to another is called type casting or type conversion.

Widening: automatic

When the new type is bigger and can hold every value of the old type, Java converts automatically. This is called widening: byte → short → int → long → float → double.

public class Main {
    public static void main(String[] args) {
        int apples = 7;
        double asDouble = apples;
        long asLong = apples;
        System.out.println(asDouble);
        System.out.println(asLong);
    }
}
Output
7.0
7

The value 7 becomes 7.0 as a double. Nothing is lost, so Java does it without asking.

Narrowing: you must ask

Going the other way, from a bigger type to a smaller one, can lose information. Java will not do it on its own; you write the new type in brackets before the value. This is a cast:

public class Main {
    public static void main(String[] args) {
        double price = 9.99;
        int whole = (int) price;
        System.out.println(whole);
        int big = 300;
        byte small = (byte) big;
        System.out.println(small);
    }
}
Output
9
44

Casting a double to an int simply cuts off the decimal part: 9.99 becomes 9, not 10. Casting 300 to a byte gives 44, because 300 does not fit in a byte and only the lowest 8 bits are kept. Cast only when you know the value fits.

Integer division and casting

When both numbers are int, division gives an int and drops the remainder. Cast one of them to double first to get the exact result:

public class Main {
    public static void main(String[] args) {
        int total = 7, count = 2;
        System.out.println(total / count);
        System.out.println((double) total / count);
        System.out.println(Math.round(2.5));
    }
}
Output
3
3.5
3

To round to the nearest whole number instead of cutting, use Math.round, which you will meet again in the Math topic.

Text to numbers and back

A number inside a String is still text: "42" + 1 gives "421". To calculate with it, convert it with Integer.parseInt or Double.parseDouble. To turn a number into text, use String.valueOf or join it with an empty string:

public class Main {
    public static void main(String[] args) {
        String text = "42";
        int number = Integer.parseInt(text);
        System.out.println(text + 1);
        System.out.println(number + 1);
        double d = Double.parseDouble("3.5");
        System.out.println(d * 2);
        String back = String.valueOf(number);
        System.out.println(back.length());
    }
}
Output
421
43
7.0
2

If the text is not a valid number, for example "forty", parseInt throws a NumberFormatException. The Exceptions topic shows how to handle that.

Practice exercises

  1. Store 9.75 in a double, cast it to an int and print both values.
  2. Compute the average of the whole numbers 4, 7 and 8 so that the result keeps its decimals.
  3. Convert the text "125" to a number, double it and print the result.