Alright, I've been programming in Java for the better part of three years, now, and consider myself very experienced. However, while looking over the Java SE source code, I ran into something I didn't expect:
好吧,我已经用Java编程三年多了,我觉得自己很有经验。然而,在查看Java SE源代码时,我遇到了一些意想不到的东西:
in class
Double
:
在课堂上双:
public static final double MIN_NORMAL = 0x1.0p-1022; // 2.2250738585072014E-308
public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324
I did not expect this and can't find out what it means. If you don't know, I'm referring to the p
and P
that are after these numbers, before the subtraction operator. I know you can use suffixes to force a number to be a double
, long
, float
, etc., but I've never encountered a p
or P
. I checked the Java API, but it doesn't mention it. Is there a complete list of Java primitive number literal modifiers somewhere? Does anyone know them all?
我没有想到这一点,也不知道这是什么意思。如果你不知道,我指的是在这些数字后面的p和p,在减法运算符之前。我知道您可以使用后缀强制一个数字为双精度、长、浮点等,但我从未遇到过p或p。在某个地方有完整的Java原始数字修饰符列表吗?有人认识他们吗?
For reference, below are the ones I've used or encountered, with the ones whose purposes elude me in bold with question marks (#
represents any arbitrary number within respective limits):
作为参考,以下是我使用过或遇到过的一些,它们的目的是用粗体用问号标记避开我(#表示在各自范围内的任意数字):
Suffixes:
后缀:
-
#
= 32-bit integerint
- # = 32位整数int
-
#L
= 64-bit integerlong
- #L = 64位整数长
#l
= another 64-bit integerl
?- #l =另一个64位整数l?
-
#f
= 32-bit floating-pointfloat
- #f = 32位浮点浮点数。
#F
= another 32-bit floating-pointfloat
?- #F =另一个32位浮点浮点浮点浮点数?
-
#d
= 64-bit floating-pointdouble
- #d = 64位浮点双精度浮点
#D
= another 64-bit floating-pointdouble
?- 另一个64位浮点双精度浮点?
-
#e#
= scientific notation - # # =科学记数法
#E#
= another scientific notation?- 另一种科学符号?
#p
= ?- # p = ?
#P
= ?- # P = ?
- Any more?
- 有更多的吗?
Prefixes:
前缀:
-
0b#
= binary (base 2) literal - 0b# =二进制(以2为基数)文字
0B#
= another binary (base 2) literal?- 0B# =另一个二进制(以2为基数)文字?
-
0#
= octal (base 8) literal - 0# =八进制(以8为基数
-
#
= decimal (base 10) literal - # =小数(以10为基数)。
-
0x#
= hexadecimal (base 16) literal - 0x# =十六进制(以16为基数)文字
0X#
= another hexadecimal (base 16) literal?- 0X# =另一个十六进制(基数16)的文字?
- Any more?
- 有更多的吗?
Other (are there suffixes or prefixes for these?):
其他(有后缀或前缀吗?)
-
(byte)#
= 8-bit integerbyte
- (字节)# = 8位整数字节
-
(short)#
= 16-bit integershort
- (短)# = 16位整数短
-
(char)#
- 32-bit characterchar
- (char)# - 32位字符char
3 个解决方案
#1
7
P is the exponent. It does not matter if it's capital or not.
P是指数。它是否是资本并不重要。
According to the Javadoc for toHextString
(which we know is being used because it begins with 0x
:
根据toHextString的Javadoc(我们知道使用toHextString是因为它以0x开头:
public static String toHexString(double d)
Returns a hexadecimal string representation of the double argument. All characters mentioned below are ASCII characters. If the argument is NaN, the result is the string "NaN". Otherwise, the result is a string that represents the sign and magnitude of the argument. If the sign is negative, the first character of the result is '-' ('\u002D'); if the sign is positive, no sign character appears in the result. As for the magnitude m:公共静态字符串toHexString(双d)返回双参数的十六进制字符串表示形式。下面提到的所有字符都是ASCII字符。如果参数是NaN,则结果是字符串“NaN”。否则,结果是一个表示参数符号和大小的字符串。如果符号为负,则结果的第一个字符为'-' ('\u002D');如果符号为正,则结果中不会出现符号字符。至于震级m:
If m is infinity, it is represented by the string "Infinity"; thus, positive infinity produces the result "Infinity" and negative
infinity produces the result "-Infinity".如果m是无穷大,则用字符串“无穷大”表示;因此,正无穷产生结果“无穷大”,负无穷产生结果“-无穷大”。
If m is zero, it is represented by the string "0x0.0p0"; thus, negative zero produces the result "-0x0.0p0" and positive zero produces the result "0x0.0p0".
如果m为0,则用字符串“0x0.0p0”表示;因此,负0产生结果“-0x0.0p0”,正0产生结果“0x0.0p0”。
If m is a double value with a normalized representation, substrings are used to represent the significand and exponent fields. The significand is represented by the characters "0x1." followed by a lowercase hexadecimal representation of the rest of the significand as a fraction. Trailing zeros in the hexadecimal representation are removed unless all the digits are zero, in which case a single zero is used. Next, the exponent is represented by "p" followed by a decimal string of the unbiased exponent as if produced by a call to Integer.toString on the exponent value.
如果m是一个具有规范化表示法的双值,则使用子字符串表示含义域和指数域。它的含义是由“0x1”字符表示,后面是小写十六进制表示其余的意义和分数。在十六进制表示法中,除非所有数字都为零,否则将删除末尾的0,在这种情况下,将使用单个0。接下来,指数用“p”表示,后面跟着无偏指数的十进制字符串,就像调用整数一样。表示指数值。
If m is a double value with a subnormal representation, the significand is represented by the characters "0x0." followed by a hexadecimal representation of the rest of the significand as a fraction. Trailing zeros in the hexadecimal representation are removed. Next, the exponent is represented by "p-1022". Note that there must be at least one nonzero digit in a subnormal significand.
如果m是一个具有次正态表示法的双值,那么它的意义数由字符“0x0.”表示,然后是一个十六进制表示法,表示其余的意义数。十六进制表示法中的后置零被删除。然后,指数用“p-1022”表示。注意,在次常态的意义数中必须至少有一个非零的数字。
According to the JLS, the following pieces of grammar are accepted:
根据JLS的规定,以下的语法是可以接受的:
3.10.1. Integer Literals
3.10.1。整型常量
IntegerTypeSuffix
:IntegerTypeSuffix:
- l
- l
- L
- l
OctalNumeral:
OctalNumeral:
- 0 OctalDigits
- 0 OctalDigits
- 0 Underscores OctalDigits
- 0强调OctalDigits
HexNumeral:
HexNumeral:
- 0 x HexDigits
- 0 x HexDigits
- 0 X HexDigits
- 0 X HexDigits
BinaryNumeral:
BinaryNumeral:
- 0 b BinaryDigits
- 0 b BinaryDigits
- 0 B BinaryDigits
- 0 B BinaryDigits
3.10.2. Floating-Point Literals
3.10.2。浮点型常量
ExponentIndicator: one of
ExponentIndicator:
- e
- e
- E
- E
FloatTypeSuffix: one of
FloatTypeSuffix:
- f
- f
- F
- F
- d
- d
- D
- D
HexSignificand:
HexSignificand:
- HexNumeral
- HexNumeral
- HexNumeral .
- HexNumeral。
- 0 x HexDigitsopt . HexDigits
- 0 x HexDigitsopt。HexDigits
- 0 X HexDigitsopt . HexDigits
- 0 X HexDigitsopt。HexDigits
BinaryExponentIndicator: one of
BinaryExponentIndicator:
- p
- p
- P
- P
No other single character literals are specified for those purposes.
没有为这些目的指定其他单个字符文字。
#2
3
All of the legal ways to declare a literal are defined in the JLS.
所有声明文字的合法方法都在JLS中定义。
-
p
orP
is the binary exponent of a number. - p或p是一个数字的二进制指数。
-
l
orL
defines along
. - l或l定义了long。
-
f
orF
defines afloat
. - f或f定义了一个浮点数。
-
d
orD
defines adouble
. - d或d定义了一个double。
-
0B
or0b
defines a binary literal. - 0B或0B定义了二进制文字。
-
0x
or0X
defines a hexadecimal literal. - 0x或0x定义十六进制文字。
-
e
orE
is also an exponent, but sincee
is a valid character in hexadecimal,p
is also used. - e或e也是一个指数,但由于e在十六进制中是一个有效字符,所以也使用p。
#3
0
P
or p
is a BinaryExponentIndicator
. See the Java language specification.
P或P是一个二元指数指示器。请参阅Java语言规范。
See http://docs.oracle.com/javase/specs/jls/se5.0/html/lexical.html#3.10.2
看到http://docs.oracle.com/javase/specs/jls/se5.0/html/lexical.html 3.10.2
#1
7
P is the exponent. It does not matter if it's capital or not.
P是指数。它是否是资本并不重要。
According to the Javadoc for toHextString
(which we know is being used because it begins with 0x
:
根据toHextString的Javadoc(我们知道使用toHextString是因为它以0x开头:
public static String toHexString(double d)
Returns a hexadecimal string representation of the double argument. All characters mentioned below are ASCII characters. If the argument is NaN, the result is the string "NaN". Otherwise, the result is a string that represents the sign and magnitude of the argument. If the sign is negative, the first character of the result is '-' ('\u002D'); if the sign is positive, no sign character appears in the result. As for the magnitude m:公共静态字符串toHexString(双d)返回双参数的十六进制字符串表示形式。下面提到的所有字符都是ASCII字符。如果参数是NaN,则结果是字符串“NaN”。否则,结果是一个表示参数符号和大小的字符串。如果符号为负,则结果的第一个字符为'-' ('\u002D');如果符号为正,则结果中不会出现符号字符。至于震级m:
If m is infinity, it is represented by the string "Infinity"; thus, positive infinity produces the result "Infinity" and negative
infinity produces the result "-Infinity".如果m是无穷大,则用字符串“无穷大”表示;因此,正无穷产生结果“无穷大”,负无穷产生结果“-无穷大”。
If m is zero, it is represented by the string "0x0.0p0"; thus, negative zero produces the result "-0x0.0p0" and positive zero produces the result "0x0.0p0".
如果m为0,则用字符串“0x0.0p0”表示;因此,负0产生结果“-0x0.0p0”,正0产生结果“0x0.0p0”。
If m is a double value with a normalized representation, substrings are used to represent the significand and exponent fields. The significand is represented by the characters "0x1." followed by a lowercase hexadecimal representation of the rest of the significand as a fraction. Trailing zeros in the hexadecimal representation are removed unless all the digits are zero, in which case a single zero is used. Next, the exponent is represented by "p" followed by a decimal string of the unbiased exponent as if produced by a call to Integer.toString on the exponent value.
如果m是一个具有规范化表示法的双值,则使用子字符串表示含义域和指数域。它的含义是由“0x1”字符表示,后面是小写十六进制表示其余的意义和分数。在十六进制表示法中,除非所有数字都为零,否则将删除末尾的0,在这种情况下,将使用单个0。接下来,指数用“p”表示,后面跟着无偏指数的十进制字符串,就像调用整数一样。表示指数值。
If m is a double value with a subnormal representation, the significand is represented by the characters "0x0." followed by a hexadecimal representation of the rest of the significand as a fraction. Trailing zeros in the hexadecimal representation are removed. Next, the exponent is represented by "p-1022". Note that there must be at least one nonzero digit in a subnormal significand.
如果m是一个具有次正态表示法的双值,那么它的意义数由字符“0x0.”表示,然后是一个十六进制表示法,表示其余的意义数。十六进制表示法中的后置零被删除。然后,指数用“p-1022”表示。注意,在次常态的意义数中必须至少有一个非零的数字。
According to the JLS, the following pieces of grammar are accepted:
根据JLS的规定,以下的语法是可以接受的:
3.10.1. Integer Literals
3.10.1。整型常量
IntegerTypeSuffix
:IntegerTypeSuffix:
- l
- l
- L
- l
OctalNumeral:
OctalNumeral:
- 0 OctalDigits
- 0 OctalDigits
- 0 Underscores OctalDigits
- 0强调OctalDigits
HexNumeral:
HexNumeral:
- 0 x HexDigits
- 0 x HexDigits
- 0 X HexDigits
- 0 X HexDigits
BinaryNumeral:
BinaryNumeral:
- 0 b BinaryDigits
- 0 b BinaryDigits
- 0 B BinaryDigits
- 0 B BinaryDigits
3.10.2. Floating-Point Literals
3.10.2。浮点型常量
ExponentIndicator: one of
ExponentIndicator:
- e
- e
- E
- E
FloatTypeSuffix: one of
FloatTypeSuffix:
- f
- f
- F
- F
- d
- d
- D
- D
HexSignificand:
HexSignificand:
- HexNumeral
- HexNumeral
- HexNumeral .
- HexNumeral。
- 0 x HexDigitsopt . HexDigits
- 0 x HexDigitsopt。HexDigits
- 0 X HexDigitsopt . HexDigits
- 0 X HexDigitsopt。HexDigits
BinaryExponentIndicator: one of
BinaryExponentIndicator:
- p
- p
- P
- P
No other single character literals are specified for those purposes.
没有为这些目的指定其他单个字符文字。
#2
3
All of the legal ways to declare a literal are defined in the JLS.
所有声明文字的合法方法都在JLS中定义。
-
p
orP
is the binary exponent of a number. - p或p是一个数字的二进制指数。
-
l
orL
defines along
. - l或l定义了long。
-
f
orF
defines afloat
. - f或f定义了一个浮点数。
-
d
orD
defines adouble
. - d或d定义了一个double。
-
0B
or0b
defines a binary literal. - 0B或0B定义了二进制文字。
-
0x
or0X
defines a hexadecimal literal. - 0x或0x定义十六进制文字。
-
e
orE
is also an exponent, but sincee
is a valid character in hexadecimal,p
is also used. - e或e也是一个指数,但由于e在十六进制中是一个有效字符,所以也使用p。
#3
0
P
or p
is a BinaryExponentIndicator
. See the Java language specification.
P或P是一个二元指数指示器。请参阅Java语言规范。
See http://docs.oracle.com/javase/specs/jls/se5.0/html/lexical.html#3.10.2
看到http://docs.oracle.com/javase/specs/jls/se5.0/html/lexical.html 3.10.2