Java – Convert Int to Long

In this tutorial, we shall learn how to convert an integer value to a long value.

Firstly, we shall go through the process of widening primitive conversion, where we shall take the advantage of implicit casting of lower datatype to higher datatypes. Secondly, we shall discuss about Integer.longValue() method, which returns this integer as a long.

1. Convert int to long using Widening Casting

Int is a smaller datatype and long is a larger datatype. So, when you assign an int value to long variable, the conversion of int to long automatically happens in Java. This is also called widening casting or widening primitive conversion.

So, to convert an int to long using widening casting, you can just assign a value of int to long.

In the following example, we have a variable n which is of int type. We shall assign i to long variable l during its declaration.

Java Program

</>
Copy
/**
 * Java Program - Convert Int to Long
 */

public class IntToLong {

	public static void main(String[] args) {
		Integer i = 125;
		long l = 125;
		System.out.println(l);
	}
	
}

Output

125

2. Convert int to long using Integer.longValue() method

Integer.longValue() performs a widening primitive conversion on this integer object and returns a long value.

In the following example, we shall take an integer object initialized to a value, and call longValue() method on this object.

Java Program

</>
Copy
/**
 * Java Program - Convert Int to Long
 */

public class IntToLong {

	public static void main(String[] args) {
		Integer i = 85;
		long l = i.longValue();
		System.out.println(l);
	}
	
}

Output

85

Conclusion

In this Java Tutorial, we learned how to write Java programs to convert an Int to Long, with the help of example Java programs.