Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Long

public final class Long
extends Number implements Comparable<Long>

java.lang.Object
   ↳ java.lang.Number
     ↳ java.lang.Long


The wrapper for the primitive type long.

Implementation note: The "bit twiddling" methods in this class use techniques described in Henry S. Warren, Jr.'s Hacker's Delight, (Addison Wesley, 2002) and Sean Anderson's Bit Twiddling Hacks.

See also:

Summary

Constants

long MAX_VALUE

Constant for the maximum long value, 263-1.

long MIN_VALUE

Constant for the minimum long value, -263.

int SIZE

Constant for the number of bits needed to represent a long in two's complement form.

Fields

public static final Class<Long> TYPE

The Class object that represents the primitive type long.

Public constructors

Long(long value)

Constructs a new Long with the specified primitive long value.

Long(String string)

Constructs a new Long from the specified string.

Public methods

static int bitCount(long v)

Counts the number of 1 bits in the specified long value; this is also referred to as population count.

byte byteValue()

Returns this object's value as a byte.

static int compare(long lhs, long rhs)

Compares two long values.

int compareTo(Long object)

Compares this object to the specified long object to determine their relative order.

static Long decode(String string)

Parses the specified string and returns a Long instance if the string can be decoded into a long value.

double doubleValue()

Returns this object's value as a double.

boolean equals(Object o)

Compares this instance with the specified object and indicates if they are equal.

float floatValue()

Returns this object's value as a float.

static Long getLong(String string, Long defaultValue)

Returns the Long value of the system property identified by string.

static Long getLong(String string, long defaultValue)

Returns the Long value of the system property identified by string.

static Long getLong(String string)

Returns the Long value of the system property identified by string.

int hashCode()

Returns an integer hash code for this object.

static long highestOneBit(long v)

Determines the highest (leftmost) bit of the specified long value that is 1 and returns the bit mask value for that bit.

int intValue()

Returns this object's value as an int.

long longValue()

Gets the primitive value of this long.

static long lowestOneBit(long v)

Determines the lowest (rightmost) bit of the specified long value that is 1 and returns the bit mask value for that bit.

static int numberOfLeadingZeros(long v)

Determines the number of leading zeros in the specified long value prior to the highest one bit.

static int numberOfTrailingZeros(long v)

Determines the number of trailing zeros in the specified long value after the lowest one bit.

static long parseLong(String string, int radix)

Parses the specified string as a signed long value using the specified radix.

static long parseLong(String string)

Parses the specified string as a signed decimal long value.

static long reverse(long v)

Reverses the order of the bits of the specified long value.

static long reverseBytes(long v)

Reverses the order of the bytes of the specified long value.

static long rotateLeft(long v, int distance)

Rotates the bits of the specified long value to the left by the specified number of bits.

static long rotateRight(long v, int distance)

Rotates the bits of the specified long value to the right by the specified number of bits.

short shortValue()

Returns this object's value as a short.

static int signum(long v)

Returns the value of the signum function for the specified long value.

static String toBinaryString(long v)

Converts the specified long value into its binary string representation.

static String toHexString(long v)

Converts the specified long value into its hexadecimal string representation.

static String toOctalString(long v)

Converts the specified long value into its octal string representation.

String toString()

Returns a string containing a concise, human-readable description of this object.

static String toString(long n)

Converts the specified long value into its decimal string representation.

static String toString(long v, int radix)

Converts the specified signed long value into a string representation based on the specified radix.

static Long valueOf(String string, int radix)

Parses the specified string as a signed long value using the specified radix.

static Long valueOf(long v)

Returns a Long instance for the specified long value.

static Long valueOf(String string)

Parses the specified string as a signed decimal long value.

Inherited methods

From class java.lang.Number
From class java.lang.Object
From interface java.lang.Comparable

Constants

MAX_VALUE

Added in API level 1
long MAX_VALUE

Constant for the maximum long value, 263-1.

Constant Value: 9223372036854775807 (0x7fffffffffffffff)

MIN_VALUE

Added in API level 1
long MIN_VALUE

Constant for the minimum long value, -263.

Constant Value: -9223372036854775808 (0x8000000000000000)

SIZE

Added in API level 1
int SIZE

Constant for the number of bits needed to represent a long in two's complement form.

Constant Value: 64 (0x00000040)

Fields

TYPE

Added in API level 1
Class<Long> TYPE

The Class object that represents the primitive type long.

Public constructors

Long

Added in API level 1
Long (long value)

Constructs a new Long with the specified primitive long value.

Parameters
value long: the primitive long value to store in the new instance.

Long

Added in API level 1
Long (String string)

Constructs a new Long from the specified string.

Parameters
string String: the string representation of a long value.
Throws
NumberFormatException if string cannot be parsed as a long value.

See also:

Public methods

bitCount

Added in API level 1
int bitCount (long v)

Counts the number of 1 bits in the specified long value; this is also referred to as population count.

Parameters
v long: the long to examine.
Returns
int the number of 1 bits in v.

byteValue

Added in API level 1
byte byteValue ()

Returns this object's value as a byte. Might involve rounding and/or truncating the value, so it fits into a byte.

Returns
byte the primitive byte value of this object.

compare

Added in API level 19
int compare (long lhs, 
                long rhs)

Compares two long values.

Parameters
lhs long
rhs long
Returns
int 0 if lhs = rhs, less than 0 if lhs < rhs, and greater than 0 if lhs > rhs.

compareTo

Added in API level 1
int compareTo (Long object)

Compares this object to the specified long object to determine their relative order.

Parameters
object Long: the long object to compare this object to.
Returns
int a negative value if the value of this long is less than the value of object; 0 if the value of this long and the value of object are equal; a positive value if the value of this long is greater than the value of object.

See also:

decode

Added in API level 1
Long decode (String string)

Parses the specified string and returns a Long instance if the string can be decoded into a long value. The string may be an optional optional sign character ("-" or "+") followed by a hexadecimal ("0x..." or "#..."), octal ("0..."), or decimal ("...") representation of a long.

Parameters
string String: a string representation of a long value.
Returns
Long a Long containing the value represented by string.
Throws
NumberFormatException if string cannot be parsed as a long value.

doubleValue

Added in API level 1
double doubleValue ()

Returns this object's value as a double. Might involve rounding.

Returns
double the primitive double value of this object.

equals

Added in API level 1
boolean equals (Object o)

Compares this instance with the specified object and indicates if they are equal. In order to be equal, o must be an instance of Long and have the same long value as this object.

Parameters
o Object: the object to compare this long with.
Returns
boolean true if the specified object is equal to this Long; false otherwise.

floatValue

Added in API level 1
float floatValue ()

Returns this object's value as a float. Might involve rounding.

Returns
float the primitive float value of this object.

getLong

Added in API level 1
Long getLong (String string, 
                Long defaultValue)

Returns the Long value of the system property identified by string. Returns the specified default value if string is null or empty, if the property can not be found or if its value can not be parsed as a long.

Parameters
string String: the name of the requested system property.
defaultValue Long: the default value that is returned if there is no long system property with the requested name.
Returns
Long the requested property's value as a Long or the default value.

getLong

Added in API level 1
Long getLong (String string, 
                long defaultValue)

Returns the Long value of the system property identified by string. Returns the specified default value if string is null or empty, if the property can not be found or if its value can not be parsed as a long.

Parameters
string String: the name of the requested system property.
defaultValue long: the default value that is returned if there is no long system property with the requested name.
Returns
Long the requested property's value as a Long or the default value.

getLong

Added in API level 1
Long getLong (String string)

Returns the Long value of the system property identified by string. Returns null if string is null or empty, if the property can not be found or if its value can not be parsed as a long.

Parameters
string String: the name of the requested system property.
Returns
Long the requested property's value as a Long or null.

hashCode

Added in API level 1
int hashCode ()

Returns an integer hash code for this object. By contract, any two objects for which equals(Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

Note that hash values must not change over time unless information used in equals comparisons also changes.

See Writing a correct hashCode method if you intend implementing your own hashCode method.

Returns
int this object's hash code.

highestOneBit

Added in API level 1
long highestOneBit (long v)

Determines the highest (leftmost) bit of the specified long value that is 1 and returns the bit mask value for that bit. This is also referred to as the Most Significant 1 Bit. Returns zero if the specified long is zero.

Parameters
v long: the long to examine.
Returns
long the bit mask indicating the highest 1 bit in v.

intValue

Added in API level 1
int intValue ()

Returns this object's value as an int. Might involve rounding and/or truncating the value, so it fits into an int.

Returns
int the primitive int value of this object.

longValue

Added in API level 1
long longValue ()

Gets the primitive value of this long.

Returns
long this object's primitive value.

lowestOneBit

Added in API level 1
long lowestOneBit (long v)

Determines the lowest (rightmost) bit of the specified long value that is 1 and returns the bit mask value for that bit. This is also referred to as the Least Significant 1 Bit. Returns zero if the specified long is zero.

Parameters
v long: the long to examine.
Returns
long the bit mask indicating the lowest 1 bit in v.

numberOfLeadingZeros

Added in API level 1
int numberOfLeadingZeros (long v)

Determines the number of leading zeros in the specified long value prior to the highest one bit.

Parameters
v long: the long to examine.
Returns
int the number of leading zeros in v.

numberOfTrailingZeros

Added in API level 1
int numberOfTrailingZeros (long v)

Determines the number of trailing zeros in the specified long value after the lowest one bit.

Parameters
v long: the long to examine.
Returns
int the number of trailing zeros in v.

parseLong

Added in API level 1
long parseLong (String string, 
                int radix)

Parses the specified string as a signed long value using the specified radix. The ASCII characters - ('-') and + ('+') are recognized as the minus and plus signs.

Parameters
string String: the string representation of a long value.
radix int: the radix to use when parsing.
Returns
long the primitive long value represented by string using radix.
Throws
NumberFormatException if string cannot be parsed as a long value, or radix < Character.MIN_RADIX || radix > Character.MAX_RADIX.

parseLong

Added in API level 1
long parseLong (String string)

Parses the specified string as a signed decimal long value. The ASCII characters - ('-') and + ('+') are recognized as the minus and plus signs.

Parameters
string String: the string representation of a long value.
Returns
long the primitive long value represented by string.
Throws
NumberFormatException if string cannot be parsed as a long value.

reverse

Added in API level 1
long reverse (long v)

Reverses the order of the bits of the specified long value.

Parameters
v long: the long value for which to reverse the bit order.
Returns
long the reversed value.

reverseBytes

Added in API level 1
long reverseBytes (long v)

Reverses the order of the bytes of the specified long value.

Parameters
v long: the long value for which to reverse the byte order.
Returns
long the reversed value.

rotateLeft

Added in API level 1
long rotateLeft (long v, 
                int distance)

Rotates the bits of the specified long value to the left by the specified number of bits.

Parameters
v long: the long value to rotate left.
distance int: the number of bits to rotate.
Returns
long the rotated value.

rotateRight

Added in API level 1
long rotateRight (long v, 
                int distance)

Rotates the bits of the specified long value to the right by the specified number of bits.

Parameters
v long: the long value to rotate right.
distance int: the number of bits to rotate.
Returns
long the rotated value.

shortValue

Added in API level 1
short shortValue ()

Returns this object's value as a short. Might involve rounding and/or truncating the value, so it fits into a short.

Returns
short the primitive short value of this object.

signum

Added in API level 1
int signum (long v)

Returns the value of the signum function for the specified long value.

Parameters
v long: the long value to check.
Returns
int -1 if v is negative, 1 if v is positive, 0 if v is zero.

toBinaryString

Added in API level 1
String toBinaryString (long v)

Converts the specified long value into its binary string representation. The returned string is a concatenation of '0' and '1' characters.

Parameters
v long: the long value to convert.
Returns
String the binary string representation of v.

toHexString

Added in API level 1
String toHexString (long v)

Converts the specified long value into its hexadecimal string representation. The returned string is a concatenation of characters from '0' to '9' and 'a' to 'f'.

Parameters
v long: the long value to convert.
Returns
String the hexadecimal string representation of l.

toOctalString

Added in API level 1
String toOctalString (long v)

Converts the specified long value into its octal string representation. The returned string is a concatenation of characters from '0' to '7'.

Parameters
v long: the long value to convert.
Returns
String the octal string representation of l.

toString

Added in API level 1
String toString ()

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

   getClass().getName() + '@' + Integer.toHexString(hashCode())

See Writing a useful toString method if you intend implementing your own toString method.

Returns
String a printable representation of this object.

toString

Added in API level 1
String toString (long n)

Converts the specified long value into its decimal string representation. The returned string is a concatenation of a minus sign if the number is negative and characters from '0' to '9'.

Parameters
n long: the long to convert.
Returns
String the decimal string representation of l.

toString

Added in API level 1
String toString (long v, 
                int radix)

Converts the specified signed long value into a string representation based on the specified radix. The returned string is a concatenation of a minus sign if the number is negative and characters from '0' to '9' and 'a' to 'z', depending on the radix. If radix is not in the interval defined by Character.MIN_RADIX and Character.MAX_RADIX then 10 is used as the base for the conversion.

This method treats its argument as signed. If you want to convert an unsigned value to one of the common non-decimal bases, you may find toBinaryString(long), #toHexString, or toOctalString(long) more convenient.

Parameters
v long: the signed long to convert.
radix int: the base to use for the conversion.
Returns
String the string representation of v.

valueOf

Added in API level 1
Long valueOf (String string, 
                int radix)

Parses the specified string as a signed long value using the specified radix.

Parameters
string String: the string representation of a long value.
radix int: the radix to use when parsing.
Returns
Long a Long instance containing the long value represented by string using radix.
Throws
NumberFormatException if string cannot be parsed as a long value, or radix < Character.MIN_RADIX || radix > Character.MAX_RADIX.

See also:

valueOf

Added in API level 1
Long valueOf (long v)

Returns a Long instance for the specified long value.

If it is not necessary to get a new Long instance, it is recommended to use this method instead of the constructor, since it maintains a cache of instances which may result in better performance.

Parameters
v long: the long value to store in the instance.
Returns
Long a Long instance containing v.

valueOf

Added in API level 1
Long valueOf (String string)

Parses the specified string as a signed decimal long value.

Parameters
string String: the string representation of a long value.
Returns
Long a Long instance containing the long value represented by string.
Throws
NumberFormatException if string cannot be parsed as a long value.

See also:

This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.