Literal in Java

A literal is a fixed value that is stored or assigned to variables. They are represented directly in code without requiring computation. Literals can be assigned to a variable of a primitive type.

Example

boolean result = true;
char c = 'a';
byte b = 100;
short s = 10000;
int i = 1000000;

Integer Literals

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.

Example: 10000004543L

Integer literals can be expressed by the following number systems:

  • Decimal — Decimal is the base-10 number system that we use every day for calculations. It consists of the numbers from 0 to 9.
  • 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.
  • 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.

In Java literals, the prefix 0x indicates hexadecimal and 0b indicates binary. For example:

The number 26, in decimal The number 26, in hexadecimal The number 26, in binary
26 0x1a 0b11010

Floating-Point Literals

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.

Example

double d1 = 123.4;

Here is the same value as d1, but in scientific notation:


double d1 = 1.234e2;
float f1 = 123.4f;

Character and String Literals

To assign char literals, use 'single quotes' and for String literals use "double quotes". The char and String Literals may contain any Unicode (UTF-16) characters. For example:

char a = '\u0001';

String a = "\u0001";

Following are some of the special escape sequences for char and String literals in Java:

Escape sequences Description
\b Backspace
\t Tab
\n Line feed
\f Form feed
\r Carriage return
\" double quote
\' Single quote
\\ Backslash

The null Literals

The null literal is used to indicate that the value or object is unavailable. It can be assigned as a value to any reference type variables. However, it cannot be assigned to variables of primitive types.

Example

String name = null;

The class literal

The class literal is formed by using a type name, followed by .class.

Example

package com.example;

public class ClassLiteralExample {

    public static void main(String[] args) {
        Class<String> a = String.class;
        Class<Long> b = long.class;
        System.out.println(a);
        System.out.println(b);
    }
}
Output
class java.lang.String
long

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.

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.