public final class Long
extends Number implements Comparable<Long>
| java.lang.Object | ||
| java.lang.Number | ||
| java.lang.Long | ||
Long类将原始类型long的值包装在对象中。 类型为Long的对象包含类型为long的单个字段。
此外,该类提供了几种将 long转换为 String和 String转换为 long ,以及其他处理 long时常用的常量和方法。
实现注意事项:“比特 highestOneBit ”方法(如 highestOneBit和 numberOfTrailingZeros )的实现基于Henry S. Warren,Jr.的 Hacker's Delight的材料 (Addison Wesley,2002)。
常量(Constants) |
|
|---|---|
int |
BYTES 以二进制补码形式表示 |
long |
MAX_VALUE 一个常数保持最大值a |
long |
MIN_VALUE 一个常数保持最小值a |
int |
SIZE 以二进制补码形式表示 |
Fields |
|
|---|---|
public static final Class<Long> |
TYPE 表示原始类型 |
Public constructors |
|
|---|---|
Long(long value) 构造一个新分配的 |
|
Long(String s) 构造一个新分配的 |
|
公共方法(Public methods) |
|
|---|---|
static int |
bitCount(long i) 返回指定 |
byte |
byteValue() 返回此值 |
static int |
compare(long x, long y) 以数字方式比较两个 |
int |
compareTo(Long anotherLong) 以数字方式比较两个 |
static Long |
decode(String nm) 将 |
double |
doubleValue() 返回此值 |
boolean |
equals(Object obj) 将此对象与指定的对象进行比较。 |
float |
floatValue() 返回此值 |
static Long |
getLong(String nm, Long val) 返回具有指定名称的系统属性的值 |
static Long |
getLong(String nm) 使用指定的名称确定系统属性的值 |
static Long |
getLong(String nm, long val) 使用指定的名称确定系统属性的值 |
int |
hashCode() 返回此 |
static int |
hashCode(long value) 返回 |
static long |
highestOneBit(long i) 在指定的 |
int |
intValue() 返回此值 |
long |
longValue() 以 |
static long |
lowestOneBit(long i) 在指定的 |
static long |
max(long a, long b) |
static long |
min(long a, long b) |
static int |
numberOfLeadingZeros(long i) 返回指定 |
static int |
numberOfTrailingZeros(long i) 返回指定 |
static long |
parseLong(String s) 将字符串参数解析为带符号的十进制数 |
static long |
parseLong(String s, int radix) 将字符串参数解析为由第二个参数指定的基数中的带符号 |
static long |
reverse(long i) 返回通过颠倒指定 |
static long |
reverseBytes(long i) 返回通过颠倒指定的 |
static long |
rotateLeft(long i, int distance) 返回通过将指定的 |
static long |
rotateRight(long i, int distance) 返回通过将指定的 |
short |
shortValue() 返回此值 |
static int |
signum(long i) 返回指定的 |
static long |
sum(long a, long b) 根据+运算符将两个 |
static String |
toBinaryString(long i) 以基数2中的无符号整数形式返回 |
static String |
toHexString(long i) 以10 |
static String |
toOctalString(long i) 以基数8中的无符号整数形式返回参数 |
static String |
toString(long i, int radix) 返回由第二个参数指定的基数中第一个参数的字符串表示形式。 |
String |
toString() 返回代表此 |
static String |
toString(long i) 返回表示指定的 |
static Long |
valueOf(String s) 返回保存指定 |
static Long |
valueOf(String s, int radix) 返回一个 |
static Long |
valueOf(long l) 返回表示指定的 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Number
|
|
java.lang.Object
|
|
java.lang.Comparable
|
|
long MAX_VALUE
一个保持最大值的常量 long可以有2 63 -1。
常量值:9223372036854775807(0x7fffffffffffffff)
long MIN_VALUE
一个常数保持最小值a long可以有-2 63 。
常量值:-9223372036854775808(0x8000000000000000)
Long (long value)
构造一个新分配的 Long对象,该对象表示指定的 long参数。
| 参数(Parameters) | |
|---|---|
value |
long: the value to be represented by the Long object. |
Long (String s)
构造一个新分配的Long对象,该对象表示由String参数指示的long值。 该字符串parseLong方法用于基数10的方式完全转换为long值。
| 参数(Parameters) | |
|---|---|
s |
String: the String to be converted to a Long. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if the String does not contain a parsable long. |
int bitCount (long i)
返回指定的long值的二进制补码二进制表示中的long 。 这个功能有时被称为人口数量 。
| 参数(Parameters) | |
|---|---|
i |
long
|
| 返回(Returns) | |
|---|---|
int |
the number of one-bits in the two's complement binary representation of the specified long value. |
byte byteValue ()
返回此值 Long为 byte 。
| 返回(Returns) | |
|---|---|
byte |
the numeric value represented by this object after conversion to type byte. |
int compare (long x,
long y)
以数字方式比较两个long值。 返回的值与以下内容返回的值相同:
Long.valueOf(x).compareTo(Long.valueOf(y))
| 参数(Parameters) | |
|---|---|
x |
long: the first long to compare |
y |
long: the second long to compare |
| 返回(Returns) | |
|---|---|
int |
the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y |
int compareTo (Long anotherLong)
以数字方式比较两个 Long对象。
| 参数(Parameters) | |
|---|---|
anotherLong |
Long: the Long to be compared. |
| 返回(Returns) | |
|---|---|
int |
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). |
Long decode (String nm)
将String解码为Long 。 接受由以下语法给出的十进制,十六进制和八进制数字:
DecimalNumeral, HexDigits, and OctalDigits are as defined in section 3.10.1 of The JavaTM Language Specification, except that underscores are not accepted between digits.
- DecodableString:
- Signopt DecimalNumeral
- Signopt
0xHexDigits- Signopt
0XHexDigits- Signopt
#HexDigits- Signopt
0OctalDigits- Sign:
-+
可选符号和/或基数说明符(“ 0x ”,“ 0X ”,“ # ”或前导零)后面的字符序列将通过Long.parseLong方法与指定的基数(10,16或8)进行分析。 这个字符序列必须表示一个正值, NumberFormatException将抛出一个NumberFormatException 。 如果指定的String第一个字符是负号,则结果将被否定。 String中不允许有空格字符。
| 参数(Parameters) | |
|---|---|
nm |
String: the String to decode. |
| 返回(Returns) | |
|---|---|
Long |
a Long object holding the long value represented by nm |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if the String does not contain a parsable long. |
也可以看看:
double doubleValue ()
返回此值 Long为 double 。
| 返回(Returns) | |
|---|---|
double |
the numeric value represented by this object after conversion to type double. |
boolean equals (Object obj)
将此对象与指定的对象进行比较。 当且仅当参数不是null并且是包含与此对象相同的long值的Long对象时,结果为true 。
| 参数(Parameters) | |
|---|---|
obj |
Object: the object to compare with. |
| 返回(Returns) | |
|---|---|
boolean |
true if the objects are the same; false otherwise. |
float floatValue ()
返回此值 Long为 float 。
| 返回(Returns) | |
|---|---|
float |
the numeric value represented by this object after conversion to type float. |
Long getLong (String nm, Long val)
返回具有指定名称的系统属性的值long 。 第一个参数被视为系统属性的名称。 系统属性可通过getProperty(java.lang.String)方法访问。 然后根据Long.decode方法将该属性的字符串值解释为long值,并返回代表该值的Long对象。
0x or the ASCII character #, not followed by a minus sign, then the rest of it is parsed as a hexadecimal integer exactly as for the method valueOf(java.lang.String, int) with radix 16. 0 followed by another character, it is parsed as an octal integer exactly as by the method valueOf(java.lang.String, int) with radix 8. valueOf(java.lang.String, int) with radix 10. 请注意,在任何情况下, L ( '\u004C' )和 l ( '\u006C' )都不允许出现在属性值的末尾作为类型指示符,这在Java编程语言源代码中是允许的。
第二个参数是默认值。 如果没有指定名称的属性,属性不具有正确的数字格式,或者指定的名称为空或null ,则返回默认值。
| 参数(Parameters) | |
|---|---|
nm |
String: property name. |
val |
Long: default value. |
| 返回(Returns) | |
|---|---|
Long |
the Long value of the property. |
Long getLong (String nm)
使用指定的名称确定系统属性的值 long 。
第一个参数被视为系统属性的名称。 系统属性可通过getProperty(java.lang.String)方法访问。 然后将该属性的字符串值解释为long值,并返回表示此值的Long对象。 可能的数字格式的细节可以在getProperty的定义中getProperty 。
如果没有指定名称的属性,如果指定的名称为空或 null ,或者属性的数字格式不正确,则返回 null 。
换句话说,该方法返回一个 Long对象,其值等于:
getLong(nm, null)
| 参数(Parameters) | |
|---|---|
nm |
String: property name. |
| 返回(Returns) | |
|---|---|
Long |
the Long value of the property. |
Long getLong (String nm, long val)
使用指定的名称确定系统属性的值 long 。
第一个参数被视为系统属性的名称。 系统属性可通过getProperty(java.lang.String)方法访问。 该属性的字符串值将被解释为long值,并返回代表该值的Long对象。 可能的数字格式的细节可以在getProperty的定义中getProperty 。
第二个参数是默认值。 如果没有指定名称的属性,属性不具有正确的数字格式,或者指定的名称为空或空值,则返回表示第二个参数值的Long对象。
换句话说,此方法返回一个 Long对象,其值等于:
getLong(nm, new Long(val))
but in practice it may be implemented in a manner such as:
to avoid the unnecessary allocation of aLong result = getLong(nm, null); return (result == null) ? new Long(val) : result;
Long object when the default value is not needed.
| 参数(Parameters) | |
|---|---|
nm |
String: property name. |
val |
long: default value. |
| 返回(Returns) | |
|---|---|
Long |
the Long value of the property. |
int hashCode ()
返回此Long的哈希码。 结果是该Long对象所拥有的原语long值的两半的异或。 也就是说,哈希码是表达式的值:
(int)(this.longValue()^(this.longValue()>>>32))
| 返回(Returns) | |
|---|---|
int |
a hash code value for this object. |
int hashCode (long value)
返回long值的哈希码; 与Long.hashCode()兼容。
| 参数(Parameters) | |
|---|---|
value |
long: the value to hash |
| 返回(Returns) | |
|---|---|
int |
a hash code value for a long value. |
long highestOneBit (long i)
在指定的long值的最高位(“最左边”)一位中返回一个long值,最多只有一位。 如果指定的值在其二进制补码表示中没有一位,即等于零,则返回零。
| 参数(Parameters) | |
|---|---|
i |
long
|
| 返回(Returns) | |
|---|---|
long |
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. |
int intValue ()
返回此值 Long为 int 。
| 返回(Returns) | |
|---|---|
int |
the numeric value represented by this object after conversion to type int. |
long longValue ()
以 long值的形式返回此 Long的值。
| 返回(Returns) | |
|---|---|
long |
the numeric value represented by this object after conversion to type long. |
long lowestOneBit (long i)
在指定的long值的最低位(“最右边”)一位中返回一个long值,最多只有一位。 如果指定的值在其二进制补码表示中没有一位,即等于零,则返回零。
| 参数(Parameters) | |
|---|---|
i |
long
|
| 返回(Returns) | |
|---|---|
long |
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. |
long max (long a,
long b)
通过调用 Math.max返回两个 long值中较大的 Math.max 。
| 参数(Parameters) | |
|---|---|
a |
long: the first operand |
b |
long: the second operand |
| 返回(Returns) | |
|---|---|
long |
the greater of a and b |
也可以看看:
long min (long a,
long b)
通过调用 Math.min返回两个 long值中较小的 Math.min 。
| 参数(Parameters) | |
|---|---|
a |
long: the first operand |
b |
long: the second operand |
| 返回(Returns) | |
|---|---|
long |
the smaller of a and b |
也可以看看:
int numberOfLeadingZeros (long i)
返回指定的long值的二进制补码表示中最高位(“最左边”)一位之前的零位数。 如果指定的值在其二进制补码表示中没有一个位,换句话说,如果它等于零,则返回64。
请注意,此方法与对数基数2密切相关。对于所有正的 long值x:
63 - numberOfLeadingZeros(x) 64 - numberOfLeadingZeros(x - 1) | 参数(Parameters) | |
|---|---|
i |
long
|
| 返回(Returns) | |
|---|---|
int |
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. |
int numberOfTrailingZeros (long i)
返回指定的long值的二进制补码表示中的最低位(“最右边”)一位后的零位数。 如果指定的值在其二进制补码表示中没有一个位,换句话说,如果它等于零,则返回64。
| 参数(Parameters) | |
|---|---|
i |
long
|
| 返回(Returns) | |
|---|---|
int |
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. |
long parseLong (String s)
将字符串参数解析为带符号的十进制数long 。 字符串中的字符必须全部为十进制数字,但第一个字符可能是ASCII减号'-' ( \u002D' )以指示负值或ASCII加号'+' ( '\u002B' )以指示正值。 返回结果long值,就好像将参数和基数10作为参数提供给parseLong(java.lang.String, int)方法一样。
请注意,字符 L ( '\u004C' )和 l ( '\u006C' )都不允许出现在字符串的末尾作为类型指示符,这在Java编程语言源代码中是允许的。
| 参数(Parameters) | |
|---|---|
s |
String: a String containing the long representation to be parsed |
| 返回(Returns) | |
|---|---|
long |
the long represented by the argument in decimal. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if the string does not contain a parsable long. |
long parseLong (String s, int radix)
将字符串参数解析为由第二个参数指定的基数中的带符号long 。 除了第一个字符可能是ASCII减号'-' ( '\u002D' )以指示负值或ASCII加号之外,字符串中的字符必须全部是指定基数的数字(由digit(char, int)返回非负值'\u002D' ) '+' ( '\u002B' )表示正值。 返回结果long值。
请注意,字符 L ( '\u004C' )和 l ( '\u006C' )都不允许作为类型指示符出现在字符串的末尾,这在Java编程语言源代码中是允许的 - 除非 L或 l可能显示为数字大于22的基数。
如果发生以下任何情况,将引发 NumberFormatException类型的异常:
null or is a string of length zero. radix is either smaller than MIN_RADIX or larger than MAX_RADIX. '-' ('\u002d') or plus sign '+' ('\u002B') provided that the string is longer than length 1. long. 例子:
parseLong("0", 10) returns 0L
parseLong("473", 10) returns 473L
parseLong("+42", 10) returns 42L
parseLong("-0", 10) returns 0L
parseLong("-FF", 16) returns -255L
parseLong("1100110", 2) returns 102L
parseLong("99", 8) throws a NumberFormatException
parseLong("Hazelnut", 10) throws a NumberFormatException
parseLong("Hazelnut", 36) returns 1356099454469L
| 参数(Parameters) | |
|---|---|
s |
String: the String containing the long representation to be parsed. |
radix |
int: the radix to be used while parsing s. |
| 返回(Returns) | |
|---|---|
long |
the long represented by the string argument in the specified radix. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if the string does not contain a parsable long. |
long reverse (long i)
返回通过颠倒指定 long值的二进制补码二进制表示中位的顺序而获得的值。
| 参数(Parameters) | |
|---|---|
i |
long
|
| 返回(Returns) | |
|---|---|
long |
the value obtained by reversing order of the bits in the specified long value. |
long reverseBytes (long i)
返回通过颠倒指定的 long值的二进制补码表示中的字节顺序而获得的值。
| 参数(Parameters) | |
|---|---|
i |
long
|
| 返回(Returns) | |
|---|---|
long |
the value obtained by reversing the bytes in the specified long value. |
long rotateLeft (long i,
int distance)
返回通过将指定的long值的二进制补码表示旋转指定的long值。 (位偏左或高位侧重新进入右侧或低位。)
请注意,具有负距离的左旋转相当于右旋: rotateLeft(val, -distance) == rotateRight(val, distance) 。 还要注意的是,以64为倍数的旋转是无操作的,因此即使距离为负值,旋转距离的最后六位也可以忽略: rotateLeft(val, distance) == rotateLeft(val, distance & 0x3F) 。
| 参数(Parameters) | |
|---|---|
i |
long
|
distance |
int
|
| 返回(Returns) | |
|---|---|
long |
the value obtained by rotating the two's complement binary representation of the specified long value left by the specified number of bits. |
long rotateRight (long i,
int distance)
返回通过将指定的long值的二进制补码表示向右旋转指定的long值。 (位右移位,或者低位侧,再次进入左边或者高位。)
请注意,具有负距离的右旋转等效于左旋: rotateRight(val, -distance) == rotateLeft(val, distance) 。 还要注意的是,以64的倍数旋转是空操作,因此即使距离为负数,旋转距离的最后六位也可以忽略: rotateRight(val, distance) == rotateRight(val, distance & 0x3F) 。
| 参数(Parameters) | |
|---|---|
i |
long
|
distance |
int
|
| 返回(Returns) | |
|---|---|
long |
the value obtained by rotating the two's complement binary representation of the specified long value right by the specified number of bits. |
short shortValue ()
返回此值 Long为 short 。
| 返回(Returns) | |
|---|---|
short |
the numeric value represented by this object after conversion to type short. |
int signum (long i)
返回指定的long值的符号函数。 (如果指定值为负,则返回值为-1;如果指定值为零,则返回0;如果指定值为正数,则返回1。)
| 参数(Parameters) | |
|---|---|
i |
long
|
| 返回(Returns) | |
|---|---|
int |
the signum function of the specified long value. |
long sum (long a,
long b)
根据+运算符将两个 long值相加。
| 参数(Parameters) | |
|---|---|
a |
long: the first operand |
b |
long: the second operand |
| 返回(Returns) | |
|---|---|
long |
the sum of a and b |
也可以看看:
String toBinaryString (long i)
以基数2中的无符号整数形式返回参数 long的字符串表示形式。
如果参数为负数, long符号long值是参数加2 64 ; 否则,它等于论证。 该值将以二进制形式(基数2)转换为一串ASCII数字,而不带额外的前导0 。 如果无符号大小为零,则用单个零字符'0' ( '\u0030' )表示; 否则,无符号大小的表示的第一个字符将不是零字符。 字符'0' ( '\u0030' )和'1' ( '\u0031' )用作二进制数字。
| 参数(Parameters) | |
|---|---|
i |
long: a long to be converted to a string. |
| 返回(Returns) | |
|---|---|
String |
the string representation of the unsigned long value represented by the argument in binary (base 2). |
String toHexString (long i)
以10 long数的形式返回 long参数的字符串表示形式,以 long为底的无符号整数。
如果参数为负数, long符号long值为参数加2 64 ; 否则,它等于论证。 该值被转换为十六进制的ASCII数字串(基数16),没有额外的前导0 s。 如果无符号大小为零,则它由单个零字符'0' ( '\u0030' )表示; 否则,无符号大小的表示的第一个字符将不是零字符。 以下字符用作十六进制数字:
0123456789abcdef
These are the characters
'\u0030' through
'\u0039' and
'\u0061' through
'\u0066'. If uppercase letters are desired, the
toUpperCase() method may be called on the result:
Long.toHexString(n).toUpperCase()
| 参数(Parameters) | |
|---|---|
i |
long: a long to be converted to a string. |
| 返回(Returns) | |
|---|---|
String |
the string representation of the unsigned long value represented by the argument in hexadecimal (base 16). |
String toOctalString (long i)
以基数8中的无符号整数形式返回 long参数的字符串表示形式。
如果参数为负数, long符号long值为参数加2 64 ; 否则,它等于论证。 该值被转换为八进制(基数8)的ASCII数字字符串,没有额外的前导0 。
如果无符号大小为零,则它由单个零字符'0' ( '\u0030' )表示; 否则,无符号大小的表示的第一个字符将不是零字符。 以下字符用作八进制数字:
01234567
These are the characters
'\u0030' through
'\u0037'.
| 参数(Parameters) | |
|---|---|
i |
long: a long to be converted to a string. |
| 返回(Returns) | |
|---|---|
String |
the string representation of the unsigned long value represented by the argument in octal (base 8). |
String toString (long i, int radix)
返回由第二个参数指定的基数中第一个参数的字符串表示形式。
如果基数小于 Character.MIN_RADIX或大于 Character.MAX_RADIX ,则使用基数 10代替。
如果第一个参数是负数,结果的第一个元素是ASCII减号'-' ( '\u002d' )。 如果第一个参数不是负数,则结果中不会出现符号字符。
结果的其余字符表示第一个参数的大小。 如果幅度为零,则用单个零字符'0' ( '\u0030' )表示; 否则,幅度表示的第一个字符将不是零字符。 以下ASCII字符用作数字:
0123456789abcdefghijklmnopqrstuvwxyz
These are
'\u0030' through
'\u0039' and
'\u0061' through
'\u007a'. If
radix is
N, then the first
N of these characters are used as radix-
N digits in the order shown. Thus, the digits for hexadecimal (radix 16) are
0123456789abcdef. If uppercase letters are desired, the
toUpperCase() method may be called on the result:
Long.toString(n, 16).toUpperCase()
| 参数(Parameters) | |
|---|---|
i |
long: a long to be converted to a string. |
radix |
int: the radix to use in the string representation. |
| 返回(Returns) | |
|---|---|
String |
a string representation of the argument in the specified radix. |
String toString ()
返回表示此Long的值的String对象。 该值将转换为带符号的十进制表示形式并返回为字符串,就好像long值作为toString(long)方法的参数toString(long) 。
| 返回(Returns) | |
|---|---|
String |
a string representation of the value of this object in base 10. |
String toString (long i)
返回表示指定的long的String对象。 该参数被转换为带符号的十进制表示并作为字符串返回,就好像参数和基数10作为toString(long, int)方法的参数toString(long, int) 。
| 参数(Parameters) | |
|---|---|
i |
long: a long to be converted. |
| 返回(Returns) | |
|---|---|
String |
a string representation of the argument in base 10. |
Long valueOf (String s)
返回保存指定的String的值的Long对象。 参数被解释为表示一个有符号的十进制数long ,就好像该参数被赋予了parseLong(java.lang.String)方法一样。 结果是一个表示由字符串指定的整数值的Long对象。
换句话说,该方法返回一个 Long对象,其值等于:
new Long(Long.parseLong(s))
| 参数(Parameters) | |
|---|---|
s |
String: the string to be parsed. |
| 返回(Returns) | |
|---|---|
Long |
a Long object holding the value represented by the string argument. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
If the string cannot be parsed as a long. |
Long valueOf (String s, int radix)
当使用第二个参数给定的基数进行分析时,返回一个保存从指定的String提取的值的Long对象。 第一个参数被解释为表示由第二个参数指定的基数中的带符号的long ,就像参数被赋予parseLong(java.lang.String, int)方法一样。 结果是一个代表字符串指定的long值的Long对象。
换句话说,该方法返回一个 Long对象,其值等于:
new Long(Long.parseLong(s, radix))
| 参数(Parameters) | |
|---|---|
s |
String: the string to be parsed |
radix |
int: the radix to be used in interpreting s |
| 返回(Returns) | |
|---|---|
Long |
a Long object holding the value represented by the string argument in the specified radix. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
If the String does not contain a parsable long. |
Long valueOf (long l)
返回表示指定的long值的Long实例。 如果不需要新的Long实例,则通常应优先使用此方法,而不是构造函数Long(long) ,因为此方法可能通过缓存频繁请求的值来产生显着更好的空间和时间性能。 请注意,与corresponding method在Integer类,这种方法不需要缓存值的特定范围内。
| 参数(Parameters) | |
|---|---|
l |
long: a long value. |
| 返回(Returns) | |
|---|---|
Long |
a Long instance representing l. |