Literal Formats for Floating-Point - Example

This section provides a tutorial example on how to use floating-point literals in 2 formats: decimal and hex.

Now let's see a tutorial program example on floating-point literals in decimal and hex formats:

/* FloatingPointLiteral.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */

class FloatingPointLiteral {
   public static void main(String[] a) {
      java.io.PrintStream o = System.out;
      o.println("Decimal Floating-Point Literals:");
      o.println("   Base part:     365.242196     = "+365.242196);
      o.println("   With exponent: 3.65242196e2   = "+3.65242196e2);
      o.println("   Negative exponent:    1.0e-9  = "+1.0e-9);
      o.println("   float type:           1.0e-9F = "+1.0e-9F);
      o.println("   Using _:     3.65_242_196e2   = "+3.65_242_196e2);

      o.println();
      o.println("Hex Floating-Point Literals:");
      o.println("   1:                     0x1.0p0  = "+0x1.0p0);
      o.println("   16:                   0x10.0p0  = "+0x10.0p0);
      o.println("   16*16:                0x10.0p0  = "+0x10.0p0);
      o.println("   1/16:                  0x0.1p0  = "+0x0.1p0);
      o.println("   1/16/16:               0x0.01p0 = "+0x0.01p0);
      o.println("   With exponent:     0x1.23456p8  = "+0x1.23456p8);
      o.println("   Negative exponent: 0x12345.6p-8 = "+0x12345.6p-8);
      o.println("   float type:    0x800000.0p0F = "+0x8000000.0p0F);
      o.println("   Using _:      0x80_0000.0p0F = "+0x80_00000.0p0F);
   }
}

Compile this example program in a command window and run it:

C:\>\progra~1\java\jdk1.8.0\bin\javac FloatingPointLiteral.java

C:\>\progra~1\java\jdk1.8.0\bin\java FloatingPointLiteral

Decimal Floating-Point Literals:
   Base part:     365.242196     = 365.242196
   With exponent: 3.65242196e2   = 365.242196
   Negative exponent:    1.0e-9  = 1.0E-9
   float type:           1.0e-9F = 1.0E-9
   Using _:     3.65_242_196e2   = 365.242196

Hex Floating-Point Literals:
   1:                     0x1.0p0  = 1.0
   16:                   0x10.0p0  = 16.0
   16*16:                0x10.0p0  = 16.0
   1/16:                  0x0.1p0  = 0.0625
   1/16/16:               0x0.01p0 = 0.00390625
   With exponent:     0x1.23456p8  = 291.27099609375
   Negative exponent: 0x12345.6p-8 = 291.27099609375
   float type:    0x800000.0p0F = 1.34217728E8
   Using _:      0x80_0000.0p0F = 1.34217728E8

Hope you know how to use floating-point literals properly now.

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

 Execution Process, Entry Point, Input and Output

Primitive Data Types and Literals

 Data Types Supported in Java

 Integer Data Types

 Floating-Point Data Types

 Logical (Boolean) Data Type

 Literals of Primitive Types

 Literal Formats for Integers

 Literal Formats for Integers - Example

 Literal Formats for Floating-Point

Literal Formats for Floating-Point - Example

 Literal Formats for Characters

 Literal Formats for Character - Example

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Outdated Tutorials

 References

 PDF Printing Version