Type casting

Widening casting(automatically)

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

Narrowing casting(manually)

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

public class Main {
    public static void main(String[] args) {
        Long myLong = 100L;
        int myInt = (int)(myLong / 10);
        short myShort = (short)(myLong / 8);
        byte myByte = (byte)(myLong / 3);
        
        System.out.println("myLong = " + myLong);
        System.out.println("myInt = " + myInt);
        System.out.println("myShort = " + myShort);
        System.out.println("myByte = " + myByte);

    }
}

The snippet result is

myLong = 100

myInt = 10

myShort = 12

myByte = 33

(type) means casting. You could use it to convert the data to the type in parentheses.

Last updated