public final class Long extends Number implements Comparable<Long>
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.
Integer,
Serialized Form| Modifier and Type | Field and Description |
|---|---|
static long |
MAX_VALUE
Constant for the maximum
long value, 263-1. |
static long |
MIN_VALUE
Constant for the minimum
long value, -263. |
static int |
SIZE
Constant for the number of bits needed to represent a
long in
two's complement form. |
static Class<Long> |
TYPE
The
Class object that represents the primitive type long. |
| Constructor and Description |
|---|
Long(long value)
Constructs a new
Long with the specified primitive long value. |
Long(String string)
Constructs a new
Long from the specified string. |
| Modifier and Type | Method and Description |
|---|---|
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 x,
long y)
Compares two
long values numerically. |
int |
compareTo(Long object)
Compares this object to the specified long object to determine their
relative order.
|
static int |
compareUnsigned(long x,
long y)
Compares two
long values numerically treating the values
as unsigned. |
static Long |
decode(String string)
Parses the specified string and returns a
Long instance if the
string can be decoded into a long value. |
static long |
divideUnsigned(long dividend,
long 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.
|
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)
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,
Long defaultValue)
Returns the
Long value of the system property identified by
string. |
int |
hashCode()
Returns an integer hash code for this object.
|
static int |
hashCode(long value)
Returns a hash code for a
long value; compatible with
Long.hashCode(). |
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 long |
max(long a,
long b)
Returns the greater of two
long values
as if by calling Math.max. |
static long |
min(long a,
long b)
Returns the smaller of two
long values
as if by calling Math.min. |
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(CharSequence s,
int beginIndex,
int endIndex,
int radix)
Parses the
CharSequence argument as a signed long in
the specified radix, beginning at the specified
beginIndex and extending to endIndex - 1. |
static long |
parseLong(String string)
Parses the specified string as a signed decimal long value.
|
static long |
parseLong(String string,
int radix)
Parses the specified string as a signed long value using the specified
radix.
|
static long |
parsePositiveLong(String string)
Equivalent to
parsePositiveLong(string, 10). |
static long |
parsePositiveLong(String string,
int radix)
Parses the specified string as a positive long value using the
specified radix.
|
static long |
remainderUnsigned(long dividend,
long 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.
|
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 long |
sum(long a,
long b)
Adds two
long values together as per the + operator. |
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 String |
toUnsignedString(long i)
Returns a string representation of the argument as an unsigned
decimal value.
|
static String |
toUnsignedString(long i,
int radix)
Returns a string representation of the first argument as an
unsigned integer value in the radix specified by the second
argument.
|
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.
|
static Long |
valueOf(String string,
int radix)
Parses the specified string as a signed long value using the specified
radix.
|
public static final long MAX_VALUE
long value, 263-1.public static final long MIN_VALUE
long value, -263.public static final int SIZE
long in
two's complement form.public Long(long value)
Long with the specified primitive long value.value - the primitive long value to store in the new instance.public Long(String string) throws NumberFormatException
Long from the specified string.string - the string representation of a long value.NumberFormatException - if string cannot be parsed as a long value.parseLong(String)public static String toUnsignedString(long i, int radix)
If the radix is smaller than Character.MIN_RADIX
or larger than Character.MAX_RADIX, then the radix
10 is used instead.
Note that since the first argument is treated as an unsigned value, no leading sign character is printed.
If the magnitude is zero, it is represented by a single zero
character '0' ('\u0030'); otherwise,
the first character of the representation of the magnitude will
not be the zero character.
The behavior of radixes and the characters used as digits
are the same as toString.
i - an integer to be converted to an unsigned string.radix - the radix to use in the string representation.toString(long, int)public byte byteValue()
Numberpublic int compareTo(Long object)
compareTo in interface Comparable<Long>object - the long object to compare this object to.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.Comparablepublic static Long decode(String string) throws NumberFormatException
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.string - a string representation of a long value.Long containing the value represented by string.NumberFormatException - if string cannot be parsed as a long value.public double doubleValue()
NumberdoubleValue in class Numberpublic boolean equals(Object o)
o must be an instance of
Long and have the same long value as this object.equals in class Objecto - the object to compare this long with.true if the specified object is equal to this
Long; false otherwise.Object.hashCode()public float floatValue()
NumberfloatValue in class Numberpublic static Long getLong(String string)
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.string - the name of the requested system property.Long or null.public static Long getLong(String string, long defaultValue)
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.string - the name of the requested system property.defaultValue - the default value that is returned if there is no long system
property with the requested name.Long or the default
value.public static Long getLong(String string, Long defaultValue)
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.string - the name of the requested system property.defaultValue - the default value that is returned if there is no long system
property with the requested name.Long or the default
value.public int hashCode()
ObjectObject.equals(java.lang.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.
hashCode in class ObjectObject.equals(java.lang.Object)public int intValue()
Numberpublic long longValue()
public static long parseLong(String string) throws NumberFormatException
string - the string representation of a long value.string.NumberFormatException - if string cannot be parsed as a long value.public static long parseLong(CharSequence s, int beginIndex, int endIndex, int radix) throws NumberFormatException
CharSequence argument as a signed long in
the specified radix, beginning at the specified
beginIndex and extending to endIndex - 1.
The method does not take steps to guard against the
CharSequence being mutated while parsing.
s - the CharSequence containing the long
representation to be parsedbeginIndex - the beginning index, inclusive.endIndex - the ending index, exclusive.radix - the radix to be used while parsing s.long represented by the subsequence in
the specified radix.NullPointerException - if s is null.IndexOutOfBoundsException - if beginIndex is
negative, or if beginIndex is greater than
endIndex or if endIndex is greater than
s.length().NumberFormatException - if the CharSequence does not
contain a parsable int in the specified
radix, or if radix is either smaller than
Character.MIN_RADIX or larger than
Character.MAX_RADIX.public static long parseLong(String string, int radix) throws NumberFormatException
string - the string representation of a long value.radix - the radix to use when parsing.string using
radix.NumberFormatException - if string cannot be parsed as a long value, or
radix < Character.MIN_RADIX ||
radix > Character.MAX_RADIX.public static long parsePositiveLong(String string) throws NumberFormatException
parsePositiveLong(string, 10).NumberFormatExceptionparsePositiveLong(String, int)public static long parsePositiveLong(String string, int radix) throws NumberFormatException
This method behaves the same as parseLong(String, int) except
that it disallows leading '+' and '-' characters. See that method for
error conditions.
NumberFormatExceptionparseLong(String, int)public short shortValue()
NumbershortValue in class Numberpublic static String toBinaryString(long v)
v - the long value to convert.v.public static String toHexString(long v)
v - the long value to convert.l.public static String toOctalString(long v)
v - the long value to convert.l.public String toString()
ObjectgetClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful
toString method
if you intend implementing your own toString method.
public static String toString(long n)
n - the long to convert.l.public static String toUnsignedString(long i)
toUnsignedString(long,
int) method.i - an integer to be converted to an unsigned string.toUnsignedString(long, int)public static String toString(long v, int radix)
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.
v - the signed long to convert.radix - the base to use for the conversion.v.public static Long valueOf(String string) throws NumberFormatException
string - the string representation of a long value.Long instance containing the long value represented by
string.NumberFormatException - if string cannot be parsed as a long value.parseLong(String)public static Long valueOf(String string, int radix) throws NumberFormatException
string - the string representation of a long value.radix - the radix to use when parsing.Long instance containing the long value represented by
string using radix.NumberFormatException - if string cannot be parsed as a long value, or
radix < Character.MIN_RADIX ||
radix > Character.MAX_RADIX.parseLong(String, int)public static long highestOneBit(long v)
v - the long to examine.v.public static long lowestOneBit(long v)
v - the long to examine.v.public static int numberOfLeadingZeros(long v)
highest one bit.v - the long to examine.v.public static int numberOfTrailingZeros(long v)
lowest one bit.v - the long to examine.v.public static int bitCount(long v)
v - the long to examine.v.public static long rotateLeft(long v,
int distance)
v - the long value to rotate left.distance - the number of bits to rotate.public static long rotateRight(long v,
int distance)
v - the long value to rotate right.distance - the number of bits to rotate.public static long reverseBytes(long v)
v - the long value for which to reverse the byte order.public static long reverse(long v)
v - the long value for which to reverse the bit order.public static int signum(long v)
signum function for the specified long
value.v - the long value to check.v is negative, 1 if v is positive, 0 if
v is zero.public static Long valueOf(long v)
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.
v - the long value to store in the instance.Long instance containing v.public static int hashCode(long value)
long value; compatible with
Long.hashCode().value - the value to hashlong value.public static int compare(long x,
long y)
long values numerically.
The value returned is identical to what would be returned by:
Long.valueOf(x).compareTo(Long.valueOf(y))
x - the first long to comparey - the second long to compare0 if x == y;
a value less than 0 if x < y; and
a value greater than 0 if x > ypublic static int compareUnsigned(long x,
long y)
long values numerically treating the values
as unsigned.x - the first long to comparey - the second long to compare0 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 valuespublic static long divideUnsigned(long dividend,
long divisor)
Note that in two's complement arithmetic, the three other
basic arithmetic operations of add, subtract, and multiply are
bit-wise identical if the two operands are regarded as both
being signed or both being unsigned. Therefore separate addUnsigned, etc. methods are not provided.
dividend - the value to be divideddivisor - the value doing the dividingremainderUnsigned(long, long)public static long remainderUnsigned(long dividend,
long divisor)
dividend - the value to be divideddivisor - the value doing the dividingdivideUnsigned(long, long)public static long sum(long a,
long b)
long values together as per the + operator.a - the first operandb - the second operanda and bBinaryOperatorpublic static long max(long a,
long b)
long values
as if by calling Math.max.a - the first operandb - the second operanda and bBinaryOperatorpublic static long min(long a,
long b)
long values
as if by calling Math.min.a - the first operandb - the second operanda and bBinaryOperator