Java – Convert Int to Long
In Java, an int can be converted to a long without losing its numeric value. The usual approach is to assign the int directly to a long variable. Java performs this conversion automatically because converting from int to long is a widening primitive conversion.
If the source value is an Integer object rather than a primitive int, you can use Integer.longValue(). This tutorial covers both forms, explicit casting, conversion to the Long wrapper class, and the behavior of int and long in arithmetic expressions.
1. Convert int to long using Widening Casting
An int is a 32-bit signed integer type, while a long is a 64-bit signed integer type. Therefore, every possible int value fits within a long. Java can perform the conversion automatically when an int is assigned to a long.
The basic syntax for converting a primitive int to long is:
int number = 125;
long result = number;
No cast is required in this assignment.
The following existing program initializes an Integer variable and a long variable with the same numeric value.
Java Program
/**
* 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
To demonstrate the widening conversion directly, assign an int variable to a long variable as shown below.
public class IntToLongWidening {
public static void main(String[] args) {
int i = 125;
long l = i;
System.out.println(l);
}
}
Output
125
Here, Java widens the value stored in i from int to long before storing it in l.
2. Convert int to long using Integer.longValue() method
The Integer.longValue() method returns the value represented by an Integer object as a primitive long. Use this method when the source variable is an Integer object and you want to request the long value explicitly.
In the following example, we shall take an integer object initialized to a value, and call longValue() method on this object.
Java Program
/**
* 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
The result is a primitive long. Java can also unbox a non-null Integer and then widen the resulting int automatically, so direct assignment is possible as well.
Integer i = 85;
long l = i;
System.out.println(l);
Output
85
3. Cast int to long explicitly in Java
You can explicitly cast an int to long by placing (long) before the value. The cast is valid, but it is normally unnecessary because Java already supports the widening conversion implicitly.
int number = 500;
long result = (long) number;
For example:
public class IntToLongCast {
public static void main(String[] args) {
int number = 500;
long result = (long) number;
System.out.println(result);
}
}
Output
500
Direct assignment, long result = number;, is simpler and produces the same result.
4. Convert int to the Long wrapper class
A primitive long and a Long object are different types. If you specifically need a Long object, you can use Long.valueOf(). The int argument is widened to long when it is passed to the method.
public class IntToLongObject {
public static void main(String[] args) {
int number = 250;
Long result = Long.valueOf(number);
System.out.println(result);
}
}
Output
250
If you already have an Integer object, one clear approach is to obtain its primitive long value and then let Java box that value as a Long if required.
Integer number = 250;
Long result = number.longValue();
System.out.println(result);
Output
250
Why int can be converted to long without data loss
Java’s int type ranges from -2147483648 to 2147483647. A long has a much wider range, from -9223372036854775808 to 9223372036854775807. Therefore, every valid int value has an exact corresponding long value.
This is why converting from int to long does not require a narrowing cast and does not truncate the value.
How int and long behave together in Java arithmetic
When a binary numeric operation combines an int and a long, Java promotes the int operand to long before performing the operation. The result of that arithmetic operation is therefore a long.
public class IntAndLongAddition {
public static void main(String[] args) {
int a = 25;
long b = 100L;
long sum = a + b;
System.out.println(sum);
}
}
Output
125
int to long versus long to int conversion
The direction of the conversion matters. Converting int to long is widening and is performed automatically. Converting long to int is narrowing because a long can contain values that do not fit in an int.
A long-to-int conversion therefore requires an explicit cast or another checked conversion approach. It should not be treated as the reverse equivalent of the safe int-to-long conversion.
Choosing the right int-to-long conversion in Java
- For a primitive
int, prefer direct assignment:long value = intValue;. - An explicit
(long)cast is allowed, but it is unnecessary for ordinaryint-to-longassignment. - For an
Integerobject, uselongValue()when you want an explicit primitive conversion. - For a
Longobject, use an approach such asLong.valueOf(intValue). - No numeric precision is lost when a Java
intis widened tolong.
In this Java Tutorial, we learned how to write Java programs to convert an Int to Long, with the help of example Java programs.
TutorialKart.com