什么是Java基本数据类型修饰符? [英] What are the Java primitive data type modifiers?

查看:146
本文介绍了什么是Java基本数据类型修饰符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,现在我从事Java编程已经有三年了,并且觉得自己非常有经验.但是,在查看Java SE源代码时,我遇到了意料之外的事情:

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

我没想到这一点,也找不到它的含义.如果您不知道,我指的是这些数字之后的pP,在减法运算符之前.我知道您可以使用后缀将数字强制为doublelongfloat等,但是我从未遇到过pP.我检查了 Java API ,但没有提及. 某处是否有Java原始数字文字修饰符的完整列表?有人知道吗?

作为参考,以下是我曾经使用或遇到的那些,目的是用问号加粗我的意思(#表示在各自范围内的任意数字):

后缀:

  • # = 32位整数int
  • #L = 64位整数long
  • #l =另一个64位整数l?
  • #f = 32位浮点float
  • #F =另一个32位浮点float?
  • #d = 64位浮点double
  • #D =另一个64位浮点double?
  • #e# =科学计数法
  • #E# =另一种科学计数法?
  • #p =吗?
  • #P =吗?
  • 还有吗?

前缀:

  • 0b# =二进制(以2为基)文字
  • 0B# =另一个二进制(基数2)文字?
  • 0# =八进制(以8为底)文字
  • # =十进制(以10为基数)文字
  • 0x# =十六进制(以16为基)文字
  • 0X# =另一个十六进制(以16为底)文字?
  • 还有吗?

其他(有后缀或前缀吗?):

  • (byte)# = 8位整数byte
  • (short)# = 16位整数short
  • (char)#-32位字符char

解决方案

P是指数.资本是否充足都没关系.

根据针对Javadoc的Javadoc toHextString (我们知道它正在使用,因为它以0x开头:

public static String toHexString(double d)返回double参数的十六进制字符串表示形式.下面提到的所有字符都是ASCII字符.如果自变量是NaN,则结果是字符串"NaN".否则,结果是一个字符串,代表参数的符号和大小.如果符号为负,则结果的第一个字符为'-'('\ u002D');如果符号为正,则结果中不显示符号字符.至于震级m:

  • 如果m为无穷大,则用字符串"Infinity"表示;因此,正无穷大产生结果无穷大".和负数
    无限会产生结果-无限".

  • 如果m为零,则由字符串"0x0.0p0&"表示;因此,负零产生结果"-0x0.0p-0".而正零则产生结果"0x0.0p0&".

  • 如果m是具有规范化表示形式的双精度值,则使用子字符串表示有效字段和指数字段.有效数字由字符"0x1"表示.随后是其余有效位数的小写十六进制表示形式(分数).除非所有数字均为零,否则将除去十六进制表示形式中的尾随零,在这种情况下,将使用单个零.接下来,指数由"p"表示.紧随其后的是无偏指数的十进制字符串,就好像是对指数值调用Integer.toString产生的一样.

  • 如果m是具有次正规表示的双值,则有效数由字符"0x0"表示.后面用十六进制表示有效位数的其余部分(分数).十六进制表示形式中的尾随零将被删除.接下来,指数由"p-1022"表示.请注意,次正规有效位数必须至少有一个非零数字.

根据 JLS ,可以接受以下语法:

3.10.1. Integer Literals

IntegerTypeSuffix:

  • l
  • L

八进制数字:

  • 0个八进制数字
  • 0下划线表示OctalDigits

HexNumeral:

  • 0 x十六进制数字
  • 0 X十六进制数字

二进制数字:

  • 0 b BinaryDigits
  • 0 B BinaryDigits

3.10.2. Floating-Point Literals

ExponentIndicator:之一

  • e
  • E

FloatTypeSuffix:之一

  • f
  • F
  • d
  • D

HexSignificand:

  • HexNumeral
  • HexNumeral.
  • 0 x HexDigitsopt.十六进制数字
  • 0 X HexDigitsopt.十六进制数字

BinaryExponentIndicator:其中之一

  • p
  • P

没有其他用于这些目的的单字符文字.

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

解决方案

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.

这篇关于什么是Java基本数据类型修饰符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆