Java Tutorials - Herong's Tutorial Examples - v8.22, by Herong Yang
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) 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:
herong> javac FloatingPointLiteral.java herong> 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.
Table of Contents
Execution Process, Entry Point, Input and Output
►Primitive Data Types and Literals
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
Java Modules - Java Package Aggregation
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
Assert Statements and -ea" Option