Data Types in Java, part 2 - specifying literals

posted 4 min read

What is a Literal? A Literal in the Java source code is used to specify values(constants). In Java, we can specify values of the primitive types, the String type and the null type. null is a special type which does not have a name, we only have a value of type null which is assignable to any reference type.

How do we specify literal value for the integral data types in Java? The JVM is a 32 bit machine. The basic integral type is int, i.e. even if we use operations on byte it would internally still be using 32 bits. How do we specify literal values of type int? Let’s look at the boolean expressions given in Listing below:

(12 == 012)
(12 == 014)

Which of the above two boolean expressions are true? The first or the second? It seems that the first is true and the second is false; no, it is the other way round. The first expression is false and the second is true, not only in Java but also in C. In fact the token 019 is not legal in a Java file Let us look at another expression.

(12 == 0x0C)

Will this expression evaluate to true? Yes, it will be true. When any token starts with ‘0x’ or ‘0X’, then the following characters are considered to be hexadecimal digits. Similarly, when a token begins with a ‘0’(digit zero), then the following characters are considered as octal digits. So in the case of Java, an in-teger constant value can be specified either using octal, decimal or hexadecimal. To specify an int literal in hexadecimal, begin the literal with either ‘0x’ or ‘0X’, followed by the hexadecimal digits for the literal value. To specify an int literal in octal, begin the literal with a ‘0’, followed by the octal digits for the literal value, and to specify an int literal in decimal, specify the literal value without starting with a zero.

From Java 7 onwards, we can specify integral literal value in binary also. If a token begins with ‘0b’ or '0B’, then the following characters are considered to be bits. There is another feature in Java 7 for integral literal value which can increase the readability of large tokens. When specifying any of the integral literal value, we can use the `_' (underscore) character for grouping, e.g. in case of binary literals, we might like to group the bits in sequences of four bits. The following two lines of code are equivalent:

int a = 0b1010_0101_1100_0011;
int a = 0b1010010111000011;

The grouping character in numeric literal cannot be used at the beginning or the end. This grouping character can also be used when specifying numeric literals in decimal, hexadecimal or octal. e.g. The following two lines of code are equivalent:

int a = 10000000;
int a = 10_000_000; // This is easier to read as ten million

If a literal of the long data type is required, then it must be suffixed by either ‘l’ (lowercase letter L) or ‘L’. ‘L’ is preferred as we tend to misread ‘l’ (lowercase letter L) as the digit ‘1’ (one). An example of long type is given below:

long tenBillion = 10_000_000_000;

Literals of the floating-point data types are also possible. When we use 7.5, it is considered as literal of type double. If we want to have a literal of type float, it should be suffixed with the letter ‘f’ or ‘F’. Example of float and double literals are given below:

float f = 7.5f;
double d = 7.5;

Literals for the char types are declared by having the single character within two single quote characters. e.g.

'A' '0' '*'

For some special characters like the single quote character, double quote character, the new line character or tab character, we use the backslash character as an escape character and then specify these characters similar to C. Table below lists the characters which can be written with the help of escape character.

Character LiteralCharacter
'`\b`'backspace
'`\f`'form feed
'`\n`'line feed
'`\r`'carriage return
'`\t`'tab character
'`\\`'back slash
'`\b'`'single quote
'`\b"`'double quote

char literals may also be specified using their octal values as in C, e.g.

char ch = '\030';

Literals for the String class are specified by enclosing a sequence of Unicode characters within double quotes("). examples of String literals are as follows:

The first String literal is a sequence of 11 characters, the second String literal above is a sequence with zero characters, or we can call it as an empty String and the third literal is a String literal with three character, which also contains the greek letter for pi. The literals for the char type and the String type, may use Unicode escapes, discussed in earlier tutorial on Data Types part 1.

Using Unicode escape the third literal above could also have been written as

"2\u03C0r".

From Java 15 onwards, String literals can also be specified using text block, where the characters of the String literal are specified using triple 'double quotes'.
This literal can span multiple lines and the characters do not need escaping.
e.g.

String code =
    """
    void main() {
        IO.println("Hello world");
    }
    """;

This tutorial is part 2 of the tutorial on Data types in Java, which covered the primitive types. This will be continued and concluded in part 3 for the data types, which will cover the reference data types.

0 votes
0 votes

More Posts

Data Types in Java, Part 1, the primitive types in detail

Pravin - Sep 15, 2025

Optimizing the Clinical Interface: Data Management for Efficient Medical Outcomes

Huifer - Jan 26

Bridging the Silence: Why Objective Data Outperforms Subjective Health Reports in Elderly Care

Huifer - Jan 27

Breaking the AI Data Bottleneck: How Hammerspace's AI Data Platform Eliminates Migration Nightmares

Tom Smithverified - Mar 16

Python Variables and Data Types

Ferdyverified - Mar 23, 2024
chevron_left

Related Jobs

View all jobs →

Commenters (This Week)

2 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!