Convert int to long in Java

Tags: int long Integer Convert

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

Assign int variable to long variable

ConvertIntToLongExample1.java

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

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

Using Long.valueOf() method to instantiate long from an int value

ConvertIntToLongExample2.java

public class ConvertIntToLongExample2 {
    public static void main(String[] args) {
        Integer value1 = 98765;
        long value2 = Long.valueOf(value1);

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

Using Integer.longValue() method to get long value from an Integer object

ConvertIntToLongExample3.java

public class ConvertIntToLongExample3 {
    public static void main(String[] args) {
        Integer value1 = 98765;
        long value2 = value1.longValue();

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

Happy Coding 😊