Literals of Primitive Types
This section describes how to use literals to represent primitive data values in Java source code. Examples are: 911, 0x38fL, '\n', 3.14F, 1e137, 0x1p3, true, etc.
What is a literal? A literal is a string representation of a data value in a Java source code.
The following list summarizes how a data value of different primitive type can be represented in literals.
- byte literal - No supported.
- short literal - No supported.
- int literal - Supported in 4 formats: decimal, hex, octal and binary.
Examples are: 911, 0x38f, 0177, and 0b1110001111.
- long literal - Supported in 4 formats: decimal, hex, octal and binary.
Examples are: 911L, 0x38fL, 0177L, and 0b1110001111L.
- char literal - Supported using quoted ASCII characters or escape sequences.
Examples are: 'x', '\'', '\n', '\177', and '\u03a9'.
- float literal - Supported in 2 formats: decimal and hex.
Examples are: 1e1F, 2.F, .3F, 0F, 3.14F, 0x1p3F, and 6.022137e23F
- double literal - Supported in 2 formats: decimal and hex.
Examples are: 1e1, 2., .3, 0.0, 3.14, 1e137, 0x1p3 and 1e-9D,
- boolean literal - Supported in 2 values: true and false.
Some interesting features supported in Java on primitive type literals:
- Underscores (_) can be used in separators to put digits into groups in integer literals and floating-point literals.
Examples are: 0xffff_ffff, 0377_7777_7777, 0b1111_1111_1111_1111_1111_1111_1111_1111, and 911_911_911_911.99999_9.
This feature does help to improve the readability of literals with many digits.
- A single leading zero (0) is used as an indicator for octal integer literals.
Examples are: 0177 and 0177L.
This is inconsistent with hex integer literals, which requires 2 leading characters (0x) as the indicator.
This feature may cause confusion because human read 0177 as a decimal value of 177, not a, octal value of 177, by ignoring the leading 0.
- Floating-point literals can represented in hexadecimal format.
Examples are: 0x1p3 is a floating-point literal in hexadecimal format for 8.0 in "double" data type.
This is very nice feature introduced in JDK 5 to allow precise and predictable specification of particular floating-point values.
- The default data type for integer literals is "int" if no suffix is used. If integer literal needs to be treated as
"long" data type, suffix "L" or "l" must be used.
This feature should not be a surprise to most developers.
- The default data type for floating-point literals is "double" if no suffix is used.
If floating-point literal needs to be treated as "float" data type, suffix "F" or "f" must be used.
This feature may be a surprise to many developers.
More syntax rules and examples of primitive type literals are provides in other sections of this chapter.
Last update: 2014.
Table of Contents
About This Book
JDK - Java Development Kit
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
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
Full Version in PDF/EPUB