CKTIS / Java
long — 64-bit integer and the Long wrapper
The primitive long type stores signed 64-bit integers. Learn the L suffix, the difference between long and Long, and the rules for value equality and hash codes.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
long value = 3_000_000_000L; Long boxed = Long.valueOf(value);long is a keyword naming a primitive type: a signed 64-bit two's-complement integer. Its range is -9223372036854775808 through 9223372036854775807, or -2^63 through 2^63 - 1. Unlike the reference type Long, long cannot hold null.
The integer literal suffix L gives a literal type long, not Long. Lowercase l is also valid, but L is easier to distinguish from the digit 1. Without a suffix, a literal has type int: long value = 3_000_000_000; does not compile; use 3_000_000_000L. Underscores between digits improve readability.
Long is an immutable wrapper class for a long value; obtain an instance with Long.valueOf(value). Long.equals(Object) returns true if and only if the argument is a Long with the same value, including when it is the very object on which the method is called. Thus, Long.valueOf(42L).equals(Integer.valueOf(42)) is false. Between two Long references, == compares object identity, not values.
Long.hashCode(long) returns an int hash computed as (int)(value ^ (value >>> 32)). The instance method Long.hashCode() produces the same result for the wrapped value. Equal values have equal hashes, but matching hashes do not prove equality. Ordinary integer operations do not signal overflow; assigning an int calculation's result to long cannot repair earlier overflow — use a long operand before calculating.
Examples
public class Main {
public static void main(String[] args) {
long value = 3_000_000_000L;
Long boxed = Long.valueOf(value);
System.out.println(value);
System.out.println(boxed.equals(Long.valueOf(value)));
System.out.println(Long.valueOf(42L).equals(Integer.valueOf(42)));
System.out.println(Long.hashCode(value));
System.out.println(boxed.hashCode() == Long.hashCode(value));
}
}Result
3000000000 true false -1294967296 true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.