Java Language Floating-point literals


Example

Floating point literals provide values that can be used where you need a float or double instance. There are three kinds of floating point literal.

  • Simple decimal forms
  • Scaled decimal forms
  • Hexadecimal forms

(The JLS syntax rules combine the two decimal forms into a single form. We treat them separately for ease of explanation.)

There are distinct literal types for float and double literals, expressed using suffixes. The various forms use letters to express different things. These letters are case insensitive.

Simple decimal forms

The simplest form of floating point literal consists of one or more decimal digits and a decimal point (.) and an optional suffix (f, F, d or D). The optional suffix allows you to specify that the literal is a float (f or F) or double (d or D) value. The default (when no suffix is specified) is double.

For example

 0.0     // this denotes zero
 .0      // this also denotes zero
 0.      // this also denotes zero
 3.14159 // this denotes Pi, accurate to (approximately!) 5 decimal places.
 1.0F    // a `float` literal
 1.0D    // a `double` literal.  (`double` is the default if no suffix is given)

In fact, decimal digits followed by a suffix is also a floating point literal.

 1F      // means the same thing as 1.0F

The meaning of a decimal literal is the IEEE floating point number that is closest to the infinite precision mathematical Real number denoted by the decimal floating point form. This conceptual value is converted to IEEE binary floating point representation using round to nearest. (The precise semantics of decimal conversion are specified in the javadocs for Double.valueOf(String) and Float.valueOf(String), bearing in mind that there are differences in the number syntaxes.)

Scaled decimal forms

Scaled decimal forms consist of simple decimal with an exponent part introduced by an E or e, and followed by a signed integer. The exponent part is a short hand for multiplying the decimal form by a power of ten, as shown in the examples below. There is also an optional suffix to distinguish float and double literals. Here are some examples:

 1.0E1    // this means 1.0 x 10^1 ... or 10.0 (double)
 1E-1D    // this means 1.0 x 10^(-1) ... or 0.1 (double)
 1.0e10f  // this means 1.0 x 10^(10) ... or 10000000000.0 (float)

The size of a literal is limited by the representation (float or double). It is a compilation error if the scale factor results in a value that is too large or too small.

Hexadecimal forms

Starting with Java 6, it is possible to express floating point literals in hexadecimal. The hexadecimal form have an analogous syntax to the simple and scaled decimal forms with the following differences:

  1. Every hexadecimal floating point literal starts with a zero (0) and then an x or X.
  2. The digits of the number (but not the exponent part!) also include the hexadecimal digits a through f and their uppercase equivalents.
  3. The exponent is mandatory, and is introduced by the letter p (or P) instead of an e or E. The exponent represents a scaling factor that is a power of 2 instead of a power of 10.

Here are some examples:

0x0.0p0f    // this is zero expressed in hexadecimal form (`float`)
0xff.0p19   // this is 255.0 x 2^19 (`double`)

Advice: since hexadecimal floating-point forms are unfamiliar to most Java programmers, it is advisable to use them sparingly.

Underscores

Starting with Java 7, underscores are permitted within the digit strings in all three forms of floating point literal. This applies to the "exponent" parts as well. See Using underscores to improve readability.

Special cases

It is a compilation error if a floating point literal denotes a number that is too large or too small to represent in the selected representation; i.e. if the number would overflow to +INF or -INF, or underflow to 0.0. However, it is legal for a literal to represent a non-zero denormalized number.

The floating point literal syntax does not provide literal representations for IEEE 754 special values such as the INF and NaN values. If you need to express them in source code, the recommended way is to use the constants defined by the java.lang.Float and java.lang.Double; e.g. Float.NaN, Float.NEGATIVE_INFINITY and Float.POSITIVE_INFINITY.