Skip to content
Veltoryx Technologies
ProLesson 2.2Beginner2 min readUpdated 30 October 2026

Type conversion and casting

When Java converts types for you, when you must cast, and the surprises of integer division and rounding.

Sometimes you have a value of one type and need another, for example an int that must become a double. Java does some conversions automatically and asks you to be explicit about others.

01Widening: automatic and safe

Moving a value into a bigger type never loses information, so Java does it for you:

text
byte → short → int → long → float → double
            char ↗
Widening.java
public class Widening {
    public static void main(String[] args) {
        int items = 3;
        long total = items;      // int → long, automatic
        double price = items;    // int → double, automatic
        System.out.println(total);
        System.out.println(price);
    }
}
Expected output
3
3.0

Loading the rest of the lesson…