Literal in Java
A literal is a fixed value that represents itself and is used directly in the code.
In Java, literals are used to assign specific values to variables or to represent constant values within the code.
In the example below, true, a, 100, 10000, 1000000 are all literals:
boolean result = true; char c = 'a'; byte b = 100; short s = 10000; int i = 1000000;
There are several types of literals that represent different values. Here are the common types of literals in Java:
- Integer Literals: Integer literals represent whole numbers without any decimal places. They can be written in the following formats:
- Floating-Point Literals: Floating-point literals represent numbers with fractional parts or exponential notations. They can be expressed in two formats: decimal (with or without exponent) or scientific (exponential notation). For example: 4.12, 123.4
- Character Literals: Character literals represent individual characters enclosed in single quotes. They can be actual characters or escape sequences. For example: 'A', 'b', '1'
To assign char literals, use 'single quotes'. The char Literals may contain any Unicode (UTF-16) characters. For example: '\u0001'
- String Literals: String literals represent sequences of characters enclosed in double quotes. They are used to represent text in Java. For example: "Hello, World!"
To assign String literals use "double quotes". The String Literals may contain any Unicode (UTF-16) characters.
- Boolean Literals: Boolean literals represent the two boolean values: true and false. They are used for logical operations and conditions. For example: true, false
- Null Literal: The null literal represents the absence of a value or a null reference. It is used to indicate that a variable does not refer to any object. For example: null
- Class Literal: The class literal is formed by using a type name, followed by .class. For example:
|Decimal||Decimal is the base-10 number system that we use every day for calculations. It consists of the numbers from 0 to 9.||26, 534|
|Hexadecimal||The numeral system that is a made up of 16 symbols, 0 through 9 and the letters A through F. It is a base 16 numerical system. It is often shortened to hex.||0x1a, 0xFF|
|Binary||The numeral system that is a made up of 2 symbols (0 and 1). It is a base 2 numerical system. The binary literals can be created in Java SE 7 and later.||0b11010|
An integer literal that ends with the letter L or l is of type long otherwise it is of type int. It is recommended to use the upper case letter L because the lower case letter l is hard to distinguish from the digit 1. For example: 10000004543L
A float value that ends with the letter F or f is a floating-point literal, otherwise it is of double type. The double literal can optionally end with the letter D or d (by convention, it is omitted). For scientific notation, the floating point types (float and double) can also be expressed using E or e.
a = String.class; Class b = long.class;
These literals are used to directly specify values of different data types in Java. They help in expressing specific values without the need for variables or computations.
Underscore Characters in Numeric Literals
Since Java 7, underscore characters _ can be inserted between digits in a numerical literal. The use of underscore between numbers may help to improve the readability of a code by separating groups of digits in numeric literals. For example:
long creditCardNumber = 1234_5678_9012_3456L; long socialSecurityNumber = 999_99_9999L; float pi = 3.14_15F;
We can place underscores only between digits; we cannot place underscores in the following places:
- Underscores cannot be inserted adjacent to a decimal point in a floating point literal. For example: float pi = 3._1415F; is invalid.
- Underscores cannot be placed prior to an F or L suffix. For example: long socialSecurityNumber = 999_99_9999_L; is invalid.
- Underscores cannot be placed in positions where a string of digits is expected.