What are the Java primitive data type modifiers?

霸气de小男生 提交于 2020-06-25 10:47:28

问题


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:

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?

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 integer int
  • #L = 64-bit integer long
  • #l = another 64-bit integer l?
  • #f = 32-bit floating-point float
  • #F = another 32-bit floating-point float?
  • #d = 64-bit floating-point double
  • #D = another 64-bit floating-point double?
  • #e# = scientific notation
  • #E# = another scientific notation?
  • #p = ?
  • #P = ?
  • Any more?

Prefixes:

  • 0b# = binary (base 2) literal
  • 0B# = another binary (base 2) literal?
  • 0# = octal (base 8) literal
  • # = decimal (base 10) literal
  • 0x# = hexadecimal (base 16) literal
  • 0X# = another hexadecimal (base 16) literal?
  • Any more?

Other (are there suffixes or prefixes for these?):

  • (byte)# = 8-bit integer byte
  • (short)# = 16-bit integer short
  • (char)# - 32-bit character char

回答1:


P is the exponent. It does not matter if it's capital or not.

According to the Javadoc for toHextString (which we know is being used because it begins with 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:

  • 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".

  • 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".

  • 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.

  • 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.

According to the JLS, the following pieces of grammar are accepted:

3.10.1. Integer Literals

IntegerTypeSuffix:

  • l
  • L

OctalNumeral:

  • 0 OctalDigits
  • 0 Underscores OctalDigits

HexNumeral:

  • 0 x HexDigits
  • 0 X HexDigits

BinaryNumeral:

  • 0 b BinaryDigits
  • 0 B BinaryDigits

3.10.2. Floating-Point Literals

ExponentIndicator: one of

  • e
  • E

FloatTypeSuffix: one of

  • f
  • F
  • d
  • D

HexSignificand:

  • HexNumeral
  • HexNumeral .
  • 0 x HexDigitsopt . HexDigits
  • 0 X HexDigitsopt . HexDigits

BinaryExponentIndicator: one of

  • p
  • P

No other single character literals are specified for those purposes.




回答2:


All of the legal ways to declare a literal are defined in the JLS.

  • p or P is the binary exponent of a number.
  • l or L defines a long.
  • f or F defines a float.
  • d or D defines a double.
  • 0B or 0b defines a binary literal.
  • 0x or 0X defines a hexadecimal literal.
  • e or E is also an exponent, but since e is a valid character in hexadecimal, p is also used.



回答3:


P or p is a BinaryExponentIndicator. See the Java language specification.

See http://docs.oracle.com/javase/specs/jls/se5.0/html/lexical.html#3.10.2



来源:https://stackoverflow.com/questions/12206819/what-are-the-java-primitive-data-type-modifiers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!