Convert long to int in Java

Tags: int long Integer Convert

In this Java core tutorial, we learn how to convert long value into int value in Java via different solutions.

How to cast long value to int value in Java

ConvertLongToIntExample1.java

public class ConvertLongToIntExample1 {
    public static void main(String[] args) {
        long value1 = 123456;
        int value2 = (int) value1;

        System.out.println("long value: " + value1);
        System.out.println("int value: " + value2);
    }
}
The output is:
long value: 123456
int value: 123456

Using Long.intValue() method to get int value from a Long object

ConvertLongToIntExample2.java

public class ConvertLongToIntExample2 {
    public static void main(String[] args) {
        Long value1 = Long.valueOf(567890);
        int value2 = value1.intValue();

        System.out.println("long value: " + value1);
        System.out.println("int value: " + value2);
    }
}
The output is:
long value: 567890
int value: 567890

Happy Coding 😊