Floating-point Literals Tutorial

Floating-point literals are simply a whole number followed by a decimal point followed by more numbers that represent a fraction of the next whole number on the number line. Floating-point literals can be either positive or negative. Floating-point literals can be assigned to variables of the following primitive data types: double, and float.
Floating-point literal structure: (number) (decimal point) (fraction of the next number on the number line)

        Number Line     ... -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 ... 
                                  ^                   ^
                                 -5.25               2.5

The double data type takes up 64 bits of memory, the float data type takes up 32 bits of memory. By default, floating-point literals are 64-bit doubles. When assigning a floating-point literal to a variable of the primitive data type float, you must include an F or f postfix letter. When assigning a floating-point literal to a variable of the primitive data type double, you can include an optional D or d postfix letter.
  • double a = 1234.75; // no postfix D required, floating-point literals are double by default
  • double b = 5_100.333_333; // note the optional use of the underscore character
  • double c = 1234.75D; // optional D suffix
  • float e = 1234.76F; // postfix F is required



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md FloatingPointLiterals
C:\Java>cd FloatingPointLiterals
C:\Java\FloatingPointLiterals>Notepad FloatingPointLiterals.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class FloatingPointLiterals {
    public static void main(String args[]) {
        double a = 1234.75;
        double b = 5_100.333_333; 
        double c = 1234.75D;
        float e = 1234.76F;
        int x = 10;
        double y = .27;
        int iResult = 0;
        double dResult = 0.0;

        iResult = (int) (x * y); // example of primitive type casting
        dResult = (x * y);

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(e);
        System.out.println("iResult = "+iResult);
        System.out.println("dResult = "+dResult);
    }
}

Now switch back to the command prompt (CMD) and type in javac FloatingPointLiterals.java and press Enter.
Now type in java FloatingPointLiterals and press Enter.


C:\Java\FloatingPointLiterals>javac FloatingPointLiterals.java
C:\Java\FloatingPointLiterals>java FloatingPointLiterals
1234.75
5100.333333
1234.75
1234.76
iResult = 2
dResult = 2.7


Final thoughts

Java floating-point literals are pretty straightforward. If you plan on writing a program that will need super precise fractional results, then it may be worth your time to research the IEEE 754 standard. The IEEE 754 standard governs the rules for binary floating-point arithmetic. It specifies number formats, basic operations, conversions, and exceptional conditions.


Tutorials