long
About
Definition: The
long
data type is a primitive data type in Java used to represent a 64-bit signed integer. It is typically used when a wider range of integer values is needed, beyond the limits of theint
type.Size: Occupies 8 bytes (64 bits) in memory.
Value Range:
-9,223,372,036,854,775,808
to9,223,372,036,854,775,807
(-2^63
to2^63 - 1
).Default Value: The default value of
long
is0L
.Wrapper Class: The wrapper class for
long
isLong
, located injava.lang
.
Characteristics of long
long
Signed Integer Representation:
The
long
type is signed, allowing both positive and negative values.
Memory Usage:
It consumes 8 bytes (64 bits) of memory, which provides a much larger range than
int
.
Default Choice for Large Numbers:
long
is chosen when working with numbers that exceed the limits of theint
type, particularly in financial calculations, timestamps, and large data processing.
Promotion in Expressions: In expressions involving
long
and other numeric types, Java promotes thelong
to a larger type (double
) when necessary, butlong
is not promoted toint
to avoid loss of precision. Example:Performance: Although
long
requires more memory (8 bytes compared toint
's 4 bytes), it is still quite efficient in most modern systems and is optimized for use in operations that require large numbers.Interoperability:
long
is used in many systems for time-based calculations (e.g., Unix timestamps), database IDs, and large counters that go beyond the capacity ofint
.
Memory
Memory Usage: Each
long
occupies 8 bytes of memory (64 bits). This allows for a significantly larger range of values compared toint
.
Operations with long
long
Arithmetic and Logical Operations
Operation
Example
Description
Arithmetic Operations
long sum = a + b;
Addition, subtraction, multiplication, division.
Comparison Operations
a == b
, a > b
, etc.
Compares two long
values.
Bitwise Operations
a & b
, `a
b`, etc.
Conversion Methods
Conversion
Method
Example
long
to String
String.valueOf(long)
String.valueOf(123456L)
→ "123456"
String
to long
Long.parseLong(String)
Long.parseLong("123456")
→ 123456
long
to int
Explicit cast (int)
(int) 123456L
→ 123456
int
to long
Implicit conversion
long largeValue = 123456;
Wrapper Class Example (Long
)
Long
)Method
Description
Long.valueOf(long l)
Returns a Long
object for the given long
value.
Long.parseLong(String s)
Parses the string argument as a long
.
Long.toString(long l)
Converts long
to its String
representation.
Long.compare(long x, y)
Compares two long
values.
Long.MIN_VALUE
Minimum value of long
(-9,223,372,036,854,775,808).
Long.MAX_VALUE
Maximum value of long
(9,223,372,036,854,775,807).
Common Mistakes
Integer Overflow: If an operation exceeds the range of
long
, it can cause overflow:Implicit Type Conversion: When performing operations involving
long
and other types, Java will promotelong
to adouble
if mixed with floating-point numbers:
Examples
Basic Example
Using long
in Arrays
long
in ArraysConverting long
to String
long
to String
Using long
in Streams
long
in StreamsBitwise Operations with long
long
Using long
with Collections (Boxed Long
)
long
with Collections (Boxed Long
)Wrapper Class Example
Last updated
Was this helpful?