Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the Java primitive data type modifiers?

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
like image 521
Ky. Avatar asked Aug 30 '12 23:08

Ky.


People also ask

What are the Java primitive data type?

Primitive types are the most basic data types available within the Java language. There are 8: boolean , byte , char , short , int , long , float and double . These types serve as the building blocks of data manipulation in Java. Such types serve only one purpose — containing pure, simple values of a kind.

What are the 5 primitive data types?

Primitive data types - includes byte , short , int , long , float , double , boolean and char.

What are the 8 primitive data types of programming?

There are 8 primitive types of data built into the Java language. These include: int, byte, short, long, float, double, boolean, and char.

What are the two main primitive data types in Java?

There are two types of data types in Java: Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double. Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.


1 Answers

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.

like image 93
corsiKa Avatar answered Sep 21 '22 07:09

corsiKa