Java Integer longValue() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

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.

Java
Integer days = 40000;
long a = days.longValue();             // 40000
long seconds = days.longValue() * 86400;   // 3456000000, no overflow
CallWhat 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
// java.lang.Integer
public long longValue()

Parameters

ParameterDescription
NonelongValue() takes no parameters.

Return Value

Return ValueDescription
longThe same number as a 64-bit long. It is exact for every Integer.

Compare with Similar Methods

Related conversions:

MethodHow it differs
intValue()Back to a plain int.
toUnsignedLong(i)Widens to long as an unsigned value, so -1 becomes 4294967295.
doubleValue()Widens to double instead.
equals(o)Explains why an Integer never equals a Long.

Java Examples

Example 1 — Multiply Large Numbers Safely with longValue

Convert to long before multiplying, and compare with plain int multiplication.

Java
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());
    }
}

How It Works

  1. Int arithmetic overflows. 2000000000 times 2 is 4000000000, which does not fit in an int, so it wraps to -294967296.
  2. Convert first. big.longValue() produces a long, so the multiplication is done in 64 bits.
  3. The right answer. The long product is the true 4000000000.
  4. 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.

Java
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);
    }
}

How It Works

  1. Start in long. days.longValue() makes the whole chain of multiplications run in 64 bits.
  2. Seconds. 40000 days is 40000 × 86400, which is 3456000000 seconds, already larger than Integer.MAX_VALUE.
  3. Milliseconds. Multiplying by 1000 gives 3456000000000, which a long holds without trouble.
  4. 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.

Java
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()));
    }
}

How It Works

  1. Sign is copied. Widening -1 to a long gives -1, because the new upper bits copy the sign bit.
  2. Unsigned is different. toUnsignedLong reads the same bits as 4294967295.
  3. Different classes. five.equals(smallFive) compares a Long with an Integer, so it is false even though both hold 5.
  4. Convert first. After longValue(), the argument boxes to a Long, so equals and == on primitives both say true.

Frequently Asked Questions

It returns the value of the Integer as a primitive long. The conversion is always exact.
Convert one operand to long before the operation: big.longValue() * 2, or (long) big * 2.
Yes. -1 becomes -1L. Use Integer.toUnsignedLong if you want the bits read as an unsigned number.
equals checks the class, and a Long is not an Integer. Compare primitives, or convert both sides with longValue().
Use Math.toIntExact(value), which throws ArithmeticException when the value does not fit.

Next: lowestOneBit()

Keep only the rightmost 1 bit of an int.

lowestOneBit() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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