Skip to main content
The long keyword in Java designates a 64-bit signed two’s complement integer primitive data type. It provides a significantly larger numerical range than the standard 32-bit int type, allocating 8 bytes of memory per variable.

Technical Specifications

  • Memory Size: 64 bits (8 bytes)
  • Minimum Value: 263-2^{63} (-9,223,372,036,854,775,808), accessible via Long.MIN_VALUE
  • Maximum Value: 26312^{63}-1 (9,223,372,036,854,775,807), accessible via Long.MAX_VALUE
  • Default Value: 0L (when declared as a class or instance variable)
  • Wrapper Class: java.lang.Long (used for generics and utility methods)

Syntax and Literals

By default, Java interprets unadorned integer literals as int. To explicitly define a long literal, you must append the suffix L or l to the number. The uppercase L is the universally accepted standard, as the lowercase l is visually indistinguishable from the digit 1 in many fonts.
long variables also support alternative base representations:

Type Casting

Because long occupies a larger memory footprint than byte, short, or int, Java performs implicit widening conversions. Conversely, converting a long down to a smaller integer type requires an explicit narrowing cast, which truncates the upper 32 bits and may result in data loss or sign inversion.

Concurrency and Atomicity

In the Java Memory Model (JMM), read and write operations on 64-bit long primitives are not guaranteed to be atomic on 32-bit architectures. The JVM may execute a 64-bit operation as two separate 32-bit operations, potentially exposing a partially updated state (word tearing) to other threads. To guarantee atomic reads and writes across all architectures, a long must be declared with the volatile modifier, or managed via the java.util.concurrent.atomic.AtomicLong class.
Tired of Poor Java Skills? Fix That With Deep Grasping!Learn More