Top Description Inners Fields Constructors Methods
java.lang

public final Class Long

extends Number
implements Comparable<Long>, Constable, ConstantDesc
Class Inheritance
All Implemented Interfaces
java.lang.constant.ConstantDesc, java.lang.constant.Constable, java.lang.Comparable
Annotations
@ValueBased
Imports
java.lang.annotation.Native, java.lang.invoke.MethodHandles, java.lang.constant.Constable, .ConstantDesc, java.math.*, java.util.Objects, .Optional, jdk.internal.misc.CDS, jdk.internal.vm.annotation.ForceInline, .IntrinsicCandidate, .Stable

The Long class wraps a value of the primitive type long in an object. An object of type Long contains a single field whose type is long.

In addition, this class provides several methods for converting a long to a String and a String to a long, as well as other constants and methods useful when dealing with a long.

This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.

Implementation Note

The implementations of the "bit twiddling" methods (such as highestOneBit and numberOfTrailingZeros) are based on material from Henry S. Warren, Jr.'s Hacker's Delight, (Addison Wesley, 2002).

Authors
Lee Boynton, Arthur van Hoff, Josh Bloch, Joseph D. Darcy
Since
1.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
public static final int
BYTES

The number of bytes used to represent a long value in two's complement binary form.

public static final long
MAX_VALUE

A constant holding the maximum value a long can have, 263-1.

public static final long
MIN_VALUE

A constant holding the minimum value a long can have, -263.

private static final long
serialVersionUID

Hides java.lang.Number.serialVersionUID.

use serialVersionUID from JDK 1.0.2 for interoperability
public static final int
SIZE

The number of bits used to represent a long value in two's complement binary form.

public static final Class<Long>
TYPE

The Class instance representing the primitive type long.

private final long
value

The value of the Long.

Constructor Summary

AccessConstructor and Description
public
Long(long
the value to be represented by the Long object.
value
)
Deprecated for removal since 9. It is rarely appropriate to use this constructor.

Constructs a newly allocated Long object that represents the specified long argument.

public
Long(String
the String to be converted to a Long.
s
)
Deprecated for removal since 9. It is rarely appropriate to use this constructor.

Constructs a newly allocated Long object that represents the long value indicated by the String parameter.

Method Summary

Modifier and TypeMethod and Description
public static int

Returns:

the number of one-bits in the two's complement binary representation of the specified long value.
bitCount
(long
the value whose bits are to be counted
i
)

Returns the number of one-bits in the two's complement binary representation of the specified long value.

public byte
byteValue()

Overrides java.lang.Number.byteValue.

Returns the value of this Long as a byte after a narrowing primitive conversion.

public static int

Returns:

the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y
compare
(long
the first long to compare
x
,
long
the second long to compare
y
)

Compares two long values numerically.

public int

Returns:

the value 0 if this Long is equal to the argument Long; a value less than 0 if this Long is numerically less than the argument Long; and a value greater than 0 if this Long is numerically greater than the argument Long (signed comparison).
compareTo
(Long
the Long to be compared.
anotherLong
)

Implements java.lang.Comparable.compareTo.

Compares two Long objects numerically.

public static int

Returns:

the value 0 if x == y; a value less than 0 if x < y as unsigned values; and a value greater than 0 if x > y as unsigned values
compareUnsigned
(long
the first long to compare
x
,
long
the second long to compare
y
)

Compares two long values numerically treating the values as unsigned.

public static long

Returns:

the compressed value
compress
(long
the value whose bits are to be compressed
i
,
long
the bit mask
mask
)

Returns the value obtained by compressing the bits of the specified long value, i, in accordance with the specified bit mask.

public static Long

Returns:

a Long object holding the long value represented by nm
decode
(String
the String to decode.
nm
)

Decodes a String into a Long.

public Optional<Long>

Returns:

an Optional describing the Long instance
describeConstable
()

Implements java.lang.constant.Constable.describeConstable.

Returns an Optional containing the nominal descriptor for this instance, which is the instance itself.

public static long

Returns:

the unsigned quotient of the first argument divided by the second argument
divideUnsigned
(long
the value to be divided
dividend
,
long
the value doing the dividing
divisor
)

Returns the unsigned quotient of dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.

public double
doubleValue()

Implements abstract java.lang.Number.doubleValue.

Returns the value of this Long as a double after a widening primitive conversion.

public boolean

Returns:

true if the objects are the same; false otherwise.
equals
(Object
the object to compare with.
obj
)

Overrides java.lang.Object.equals.

Compares this object to the specified object.

public static long

Returns:

the expanded value
expand
(long
the value whose bits are to be expanded
i
,
long
the bit mask
mask
)

Returns the value obtained by expanding the bits of the specified long value, i, in accordance with the specified bit mask.

public float
floatValue()

Implements abstract java.lang.Number.floatValue.

Returns the value of this Long as a float after a widening primitive conversion.

private static void
formatUnsignedLong0(long
the unsigned long to format
val
,
int
the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
shift
,
byte[]
the byte buffer to write to
buf
,
int
the offset in the destination buffer to start at
offset
,
int
the number of characters to write
len
)

Format a long (treated as unsigned) into a byte buffer (LATIN1 version).

private static void
formatUnsignedLong0UTF16(long
the unsigned long to format
val
,
int
the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
shift
,
byte[]
the byte buffer to write to
buf
,
int
the offset in the destination buffer to start at
offset
,
int
the number of characters to write
len
)

Format a long (treated as unsigned) into a byte buffer (UTF16 version).

public static Long

Returns:

the Long value of the property.
getLong
(String
property name.
nm
)

Determines the long value of the system property with the specified name.

public static Long

Returns:

the Long value of the property.
getLong
(String
property name.
nm
,
long
default value.
val
)

Determines the long value of the system property with the specified name.

public static Long

Returns:

the Long value of the property.
getLong
(String
property name.
nm
,
Long
default value.
val
)

Returns the long value of the system property with the specified name.

public int

Returns:

a hash code value for this object.
hashCode
()

Overrides java.lang.Object.hashCode.

Returns a hash code for this Long.

public static int

Returns:

a hash code value for a long value.
hashCode
(long
the value to hash
value
)

Returns a hash code for a long value; compatible with Long.hashCode().

public static long

Returns:

a long value with a single one-bit, in the position of the highest-order one-bit in the specified value, or zero if the specified value is itself equal to zero.
highestOneBit
(long
the value whose highest one bit is to be computed
i
)

Returns a long value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit in the specified long value.

public int
intValue()

Implements abstract java.lang.Number.intValue.

Returns the value of this Long as an int after a narrowing primitive conversion.

public long
longValue()

Implements abstract java.lang.Number.longValue.

Returns the value of this Long as a long value.

public static long

Returns:

a long value with a single one-bit, in the position of the lowest-order one-bit in the specified value, or zero if the specified value is itself equal to zero.
lowestOneBit
(long
the value whose lowest one bit is to be computed
i
)

Returns a long value with at most a single one-bit, in the position of the lowest-order ("rightmost") one-bit in the specified long value.

public static long

Returns:

the greater of a and b
max
(long
the first operand
a
,
long
the second operand
b
)

Returns the greater of two long values as if by calling Math.max.

public static long

Returns:

the smaller of a and b
min
(long
the first operand
a
,
long
the second operand
b
)

Returns the smaller of two long values as if by calling Math.min.

public static int

Returns:

the number of zero bits preceding the highest-order ("leftmost") one-bit in the two's complement binary representation of the specified long value, or 64 if the value is equal to zero.
numberOfLeadingZeros
(long
the value whose number of leading zeros is to be computed
i
)

Returns the number of zero bits preceding the highest-order ("leftmost") one-bit in the two's complement binary representation of the specified long value.

public static int

Returns:

the number of zero bits following the lowest-order ("rightmost") one-bit in the two's complement binary representation of the specified long value, or 64 if the value is equal to zero.
numberOfTrailingZeros
(long
the value whose number of trailing zeros is to be computed
i
)

Returns the number of zero bits following the lowest-order ("rightmost") one-bit in the two's complement binary representation of the specified long value.

private static long
parallelSuffix(long maskCount)

public static long

Returns:

the long represented by the string argument in the specified radix.
parseLong
(String
the String containing the long representation to be parsed.
s
,
int
the radix to be used while parsing s.
radix
)

Parses the string argument as a signed long in the radix specified by the second argument.

public static long

Returns:

the signed long represented by the subsequence in the specified radix.
parseLong
(CharSequence
the CharSequence containing the long representation to be parsed
s
,
int
the beginning index, inclusive.
beginIndex
,
int
the ending index, exclusive.
endIndex
,
int
the radix to be used while parsing s.
radix
)

Parses the CharSequence argument as a signed long in the specified radix, beginning at the specified beginIndex and extending to endIndex - 1.

public static long

Returns:

the long represented by the argument in decimal.
parseLong
(String
a String containing the long representation to be parsed
s
)

Parses the string argument as a signed decimal long.

public static long

Returns:

the unsigned long represented by the string argument in the specified radix.
parseUnsignedLong
(String
the String containing the unsigned integer representation to be parsed
s
,
int
the radix to be used while parsing s.
radix
)

Parses the string argument as an unsigned long in the radix specified by the second argument.

public static long

Returns:

the unsigned long represented by the subsequence in the specified radix.
parseUnsignedLong
(CharSequence
the CharSequence containing the unsigned long representation to be parsed
s
,
int
the beginning index, inclusive.
beginIndex
,
int
the ending index, exclusive.
endIndex
,
int
the radix to be used while parsing s.
radix
)

Parses the CharSequence argument as an unsigned long in the specified radix, beginning at the specified beginIndex and extending to endIndex - 1.

public static long

Returns:

the unsigned long value represented by the decimal string argument
parseUnsignedLong
(String
a String containing the unsigned long representation to be parsed
s
)

Parses the string argument as an unsigned decimal long.

public static long

Returns:

the unsigned remainder of the first argument divided by the second argument
remainderUnsigned
(long
the value to be divided
dividend
,
long
the value doing the dividing
divisor
)

Returns the unsigned remainder from dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.

public Long

Returns:

the Long instance
resolveConstantDesc
(MethodHandles.Lookup
ignored
lookup
)

Implements java.lang.constant.ConstantDesc.resolveConstantDesc.

Resolves this instance as a ConstantDesc, the result of which is the instance itself.

public static long

Returns:

the value obtained by reversing order of the bits in the specified long value.
reverse
(long
the value to be reversed
i
)

Returns the value obtained by reversing the order of the bits in the two's complement binary representation of the specified long value.

public static long

Returns:

the value obtained by reversing the bytes in the specified long value.
reverseBytes
(long
the value whose bytes are to be reversed
i
)

Returns the value obtained by reversing the order of the bytes in the two's complement representation of the specified long value.

public static long

Returns:

the value obtained by rotating the two's complement binary representation of the specified long value left by the specified number of bits.
rotateLeft
(long
the value whose bits are to be rotated left
i
,
int
the number of bit positions to rotate left
distance
)

Returns the value obtained by rotating the two's complement binary representation of the specified long value left by the specified number of bits.

public static long

Returns:

the value obtained by rotating the two's complement binary representation of the specified long value right by the specified number of bits.
rotateRight
(long
the value whose bits are to be rotated right
i
,
int
the number of bit positions to rotate right
distance
)

Returns the value obtained by rotating the two's complement binary representation of the specified long value right by the specified number of bits.

public short
shortValue()

Overrides java.lang.Number.shortValue.

Returns the value of this Long as a short after a narrowing primitive conversion.

public static int

Returns:

the signum function of the specified long value.
signum
(long
the value whose signum is to be computed
i
)

Returns the signum function of the specified long value.

pack-priv static int

Returns:

string size
stringSize
(long
long value
x
)

Returns the string representation size for a given long value.

public static long

Returns:

the sum of a and b
sum
(long
the first operand
a
,
long
the second operand
b
)

Adds two long values together as per the + operator.

public static String

Returns:

the string representation of the unsigned long value represented by the argument in binary (base 2).
toBinaryString
(long
a long to be converted to a string.
i
)

Returns a string representation of the long argument as an unsigned integer in base 2.

public static String

Returns:

the string representation of the unsigned long value represented by the argument in hexadecimal (base 16).
toHexString
(long
a long to be converted to a string.
i
)

Returns a string representation of the long argument as an unsigned integer in base 16.

public static String

Returns:

the string representation of the unsigned long value represented by the argument in octal (base 8).
toOctalString
(long
a long to be converted to a string.
i
)

Returns a string representation of the long argument as an unsigned integer in base 8.

public static String

Returns:

a string representation of the argument in the specified radix.
toString
(long
a long to be converted to a string.
i
,
int
the radix to use in the string representation.
radix
)

Returns a string representation of the first argument in the radix specified by the second argument.

public static String

Returns:

a string representation of the argument in base 10.
toString
(long
a long to be converted.
i
)

Returns a String object representing the specified long.

public String

Returns:

a string representation of the value of this object in base 10.
toString
()

Overrides java.lang.Object.toString.

Returns a String object representing this Long's value.

private static String
toStringUTF16(long i, int radix)

private static BigInteger
toUnsignedBigInteger(long i)

Return a BigInteger equal to the unsigned value of the argument.

public static String

Returns:

an unsigned string representation of the argument in the specified radix.
toUnsignedString
(long
an integer to be converted to an unsigned string.
i
,
int
the radix to use in the string representation.
radix
)

Returns a string representation of the first argument as an unsigned integer value in the radix specified by the second argument.

public static String

Returns:

an unsigned string representation of the argument.
toUnsignedString
(long
an integer to be converted to an unsigned string.
i
)

Returns a string representation of the argument as an unsigned decimal value.

pack-priv static String
toUnsignedString0(long
the value to format
val
,
int
the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
shift
)

Format a long (treated as unsigned) into a String.

public static Long

Returns:

a Long object holding the value represented by the string argument in the specified radix.
valueOf
(String
the string to be parsed
s
,
int
the radix to be used in interpreting s
radix
)

Returns a Long object holding the value extracted from the specified String when parsed with the radix given by the second argument.

public static Long

Returns:

a Long object holding the value represented by the string argument.
valueOf
(String
the string to be parsed.
s
)

Returns a Long object holding the value of the specified String.

public static Long

Returns:

a Long instance representing l.
valueOf
(long
a long value.
l
)

Returns a Long instance representing the specified long value.