long
About
Definition: The
longdata 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 theinttype.Size: Occupies 8 bytes (64 bits) in memory.
Value Range:
-9,223,372,036,854,775,808to9,223,372,036,854,775,807(-2^63to2^63 - 1).Default Value: The default value of
longis0L.Wrapper Class: The wrapper class for
longisLong, located injava.lang.
Characteristics of long
longSigned Integer Representation:
The
longtype 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:
longis chosen when working with numbers that exceed the limits of theinttype, particularly in financial calculations, timestamps, and large data processing.
Promotion in Expressions: In expressions involving
longand other numeric types, Java promotes thelongto a larger type (double) when necessary, butlongis not promoted tointto avoid loss of precision. Example:long a = 10L; double result = a / 2.0; // Promoted to doublePerformance: Although
longrequires 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:
longis 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
longoccupies 8 bytes of memory (64 bits). This allows for a significantly larger range of values compared toint.
Operations with long
longArithmetic 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:long max = Long.MAX_VALUE; max = max + 1; // Overflow System.out.println(max); // Output: -9223372036854775808Implicit Type Conversion: When performing operations involving
longand other types, Java will promotelongto adoubleif mixed with floating-point numbers:long a = 10L; double b = 5.5; double result = a + b; // Promoted to double System.out.println(result); // Output: 15.5
Examples
Basic Example
public class LongExample {
public static void main(String[] args) {
long num1 = 100000L;
long num2 = 200000L;
// Arithmetic operations
long sum = num1 + num2;
System.out.println("Sum: " + sum); // Sum: 300000
// Comparing values
System.out.println("Is num1 greater than num2? " + (num1 > num2)); // Is num1 greater than num2? false
// Casting
int smallerValue = (int) num1; // Cast long to int
System.out.println("Casted Value: " + smallerValue); // Casted Value: 100000
}
}Using long in Arrays
long in Arrayspublic class LongArrayExample {
public static void main(String[] args) {
long[] longArray = {10L, 20L, 30L};
for (long value : longArray) {
System.out.println(value); // 10, 20, 30
}
}
}Converting long to String
long to Stringpublic class LongConversion {
public static void main(String[] args) {
long num = 123456L;
String str = Long.toString(num);
System.out.println("Long as String: " + str); // Long as String: 123456
}
}Using long in Streams
long in Streamsimport java.util.stream.LongStream;
public class LongStreamExample {
public static void main(String[] args) {
long[] longArray = {100L, 200L, 300L};
LongStream.of(longArray)
.map(x -> x * 2)
.forEach(System.out::println); // 200, 400, 600
}
}Bitwise Operations with long
longpublic class LongBitwiseExample {
public static void main(String[] args) {
long a = 5L; // 0101 in binary
long b = 3L; // 0011 in binary
System.out.println("Bitwise AND: " + (a & b)); // Bitwise AND: 1
System.out.println("Bitwise OR: " + (a | b)); // Bitwise OR: 7
System.out.println("Bitwise XOR: " + (a ^ b)); // Bitwise XOR: 6
}
}Using long with Collections (Boxed Long)
long with Collections (Boxed Long)import java.util.*;
public class LongCollectionExample {
public static void main(String[] args) {
List<Long> list = Arrays.asList(100L, 200L, 300L);
list.forEach(System.out::println); // 100, 200, 300
}
}Wrapper Class Example
public class LongWrapperExample {
public static void main(String[] args) {
String strValue = "123456789";
long parsedValue = Long.parseLong(strValue);
System.out.println("Parsed Value: " + parsedValue); // Parsed Value: 123456789
long minValue = Long.MIN_VALUE;
long maxValue = Long.MAX_VALUE;
System.out.println("Min Value: " + minValue); // Min Value: -9223372036854775808
System.out.println("Max Value: " + maxValue); // Max Value: 9223372036854775807
}
}Last updated