Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

The Long class

Reference for java.lang.Long: the long wrapper, constants, parsing, L suffix, hashing, unsigned helpers, boxing and caching, and overflow behavior.

Long wraps the 64-bit primitive long in an object and bundles static helpers for parsing, formatting, comparing and bit work. A long holds values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, which is plenty for timestamps in milliseconds, database ids, file sizes and counters that would overflow an int.

Everything that applies to Integer applies here too: the class is immutable, extends Number, implements Comparable<Long>, and is boxed and unboxed automatically. The two most common mistakes are forgetting the L suffix on literals and comparing boxed values with ==.

Java
long big = 3_000_000_000L;   // L is required: 3_000_000_000 is not an int
Long boxed = big;

Try it

Java Constants, parsing and the L suffix

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Narrowing, overflow and boxed comparison

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public final class Long extends Number implements Comparable<Long>
Member Description
MAX_VALUE, MIN_VALUE The extreme values of long.
SIZE, BYTES 64 and 8.
valueOf(long), valueOf(String) Returns a Long; -128 to 127 are cached.
parseLong(String), parseLong(String, int radix) Text to long; throws NumberFormatException.
toString(long), toBinaryString, toHexString, toOctalString Conversion to text, unsigned for binary/hex/octal.
compare(long, long), compareTo(Long) Ordering.
sum, max, min Method references for reductions.
bitCount, numberOfLeadingZeros, numberOfTrailingZeros, rotateLeft, reverse Bit operations.
hashCode(long) (int) (value ^ (value >>> 32)).
parseUnsignedLong, toUnsignedString, divideUnsigned, remainderUnsigned, compareUnsigned Unsigned interpretation.

Throws: NumberFormatException for bad text; NullPointerException when unboxing null.

Usage notes

  • Literals need L. long x = 3000000000; does not compile ("integer number too large"). Prefer the uppercase L; lowercase l looks like 1.
  • Mixed arithmetic. int * int is computed in int and may overflow before being widened: long ms = 60 * 60 * 24 * 365 * 1000 is wrong. Make one operand long.
  • == on Long objects compares identity; only -128..127 are cached. Use equals() or unbox.
  • equals is type-strict. Long.valueOf(7).equals(7) is false, because the argument is boxed to Integer. Write 7L.
  • Atomicity. Reads and writes of a plain long are not guaranteed atomic on 32-bit JVMs unless the field is volatile. Use AtomicLong or LongAdder for shared counters.
  • Timestamps. System.currentTimeMillis() returns a long. For dates prefer java.time types.
  • Overflow wraps silently, like int. Use the Math exact methods or BigInteger where it matters.
  • Precision loss. Converting a long above 2^53 to double rounds it, so do money and ids in long or BigDecimal, never double.