Home Java Integer Methods longValue() Java Integer longValue() Method Definition In Java, boxed.longValue() returns the number inside an Integer as a primitive long. Every int fits in a long, so nothing is lost.
Use it to start a calculation in long arithmetic. A long can hold values up to about 9.2 quintillion, so products and sums that would overflow an int stay correct.
An instance method of java.lang.Integer: call it on an Integer object. Integer lives in java.lang, so no import is needed.
Syntax Call it on an Integer. It takes no arguments.
Integer days = 40000;
long a = days.longValue(); // 40000
long seconds = days.longValue() * 86400; // 3456000000, no overflow Call What it does boxed.longValue()The value as a long. (long) boxedA cast does the same for an Integer (it unboxes and widens). Integer.toUnsignedLong(i)Widens the bits as an unsigned number instead. Math.toIntExact(l)The opposite direction, which throws if the value does not fit.
Widening keeps the sign: a negative Integer gives a negative long. Convert before you multiply, not after.
Library declarations // java.lang.Integer
public long longValue() Parameters Parameter Description None longValue() takes no parameters.
Return Value Return Value Description longThe same number as a 64-bit long. It is exact for every Integer.
Important
The conversion must happen before the arithmetic: days.longValue() * 86400 is correct, but days * 86400 is calculated in int first and may already have overflowed. Also remember that Long.valueOf(5).equals(Integer.valueOf(5)) is false because the classes differ; compare with longValue() on both sides, or compare primitives.
Compare with Similar Methods Related conversions:
Java Examples 1. Avoid int overflow 2. Days to seconds 3. Signs and Long equality Example 1 — Multiply Large Numbers Safely with longValue Convert to long before multiplying, and compare with plain int multiplication.
public class IntegerLongValueDemo {
public static void main(String[] args) {
Integer big = 2000000000;
System.out.println("int * 2: " + (big * 2));
System.out.println("longValue * 2: " + (big.longValue() * 2));
System.out.println("original long: " + big.longValue());
}
} int * 2: -294967296
longValue * 2: 4000000000
original long: 2000000000 How It Works Int arithmetic overflows. 2000000000 times 2 is 4000000000, which does not fit in an int, so it wraps to -294967296.Convert first. big.longValue() produces a long, so the multiplication is done in 64 bits.The right answer. The long product is the true 4000000000.The value itself is unchanged. Widening 2000000000 gives the same number as a long.Example 2 — Convert Days to Seconds and Milliseconds Use longValue() to turn a day count into seconds and milliseconds without overflow.
public class IntegerLongValueSeconds {
public static void main(String[] args) {
Integer days = 40000;
long seconds = days.longValue() * 24 * 60 * 60;
long millis = seconds * 1000;
int wrong = days * 24 * 60 * 60;
System.out.println("seconds: " + seconds);
System.out.println("milliseconds: " + millis);
System.out.println("with int: " + wrong);
}
} seconds: 3456000000
milliseconds: 3456000000000
with int: -838967296 How It Works Start in long. days.longValue() makes the whole chain of multiplications run in 64 bits.Seconds. 40000 days is 40000 × 86400, which is 3456000000 seconds, already larger than Integer.MAX_VALUE.Milliseconds. Multiplying by 1000 gives 3456000000000, which a long holds without trouble.The int version is wrong. The same product in int arithmetic wraps around and prints -838967296.Example 3 — Watch Signs and Long Equality See that widening keeps the sign, and why Long.equals(Integer) is false.
public class IntegerLongValueEquals {
public static void main(String[] args) {
Integer minusOne = -1;
System.out.println("widened: " + minusOne.longValue());
System.out.println("unsigned: " + Integer.toUnsignedLong(minusOne));
Long five = 5L;
Integer smallFive = 5;
System.out.println("Long equals Integer: " + five.equals(smallFive));
System.out.println("Long equals longValue(): " + five.equals(smallFive.longValue()));
System.out.println("primitives equal: " + (five.longValue() == smallFive.longValue()));
}
} widened: -1
unsigned: 4294967295
Long equals Integer: false
Long equals longValue(): true
primitives equal: true How It Works Sign is copied. Widening -1 to a long gives -1, because the new upper bits copy the sign bit.Unsigned is different. toUnsignedLong reads the same bits as 4294967295.Different classes. five.equals(smallFive) compares a Long with an Integer, so it is false even though both hold 5.Convert first. After longValue(), the argument boxes to a Long, so equals and == on primitives both say true.Frequently Asked Questions What does Integer.longValue() do? It returns the value of the Integer as a primitive long. The conversion is always exact.
How do I avoid int overflow when multiplying? Convert one operand to long before the operation: big.longValue() * 2, or (long) big * 2.
Does longValue() keep the sign of a negative number? Yes. -1 becomes -1L. Use Integer.toUnsignedLong if you want the bits read as an unsigned number.
Why is Long.equals(Integer) false for the same number? equals checks the class, and a Long is not an Integer. Compare primitives, or convert both sides with longValue().
How do I convert a long back to an int safely? Use Math.toIntExact(value), which throws ArithmeticException when the value does not fit.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful
Helpful Share Copy link Suggestion