Literals in Java

In Java, literal is a notation that represents a fixed value in the source code. In lexical analysis, literals of a given type are generally known as tokens. In this section, we will discuss the term literals in Java.

Literals

In Java, literals are the constant values that appear directly in the program. It can be assigned directly to a variable. Java has various types of literals. The following figure represents a literal.

Literals in Java

Types of Literals in Java

There are the majorly four types of literals in Java:

  1. Integer Literal
  2. Character Literal
  3. Boolean Literal
  4. String Literal
Literals in Java

Integer Literals

Integer literals are sequences of digits. There are three types of integer literals:

  • Decimal Integer: These are the set of numbers that consist of digits from 0 to 9. It may have a positive (+) or negative (-) Note that between numbers commas and non-digit characters are not permitted. For example, 5678, +657, -89, etc.
  • Octal Integer: It is a combination of number have digits from 0 to 7 with a leading 0. For example, 045, 026,
  • Hexa-Decimal: The sequence of digits preceded by 0x or 0X is considered as hexadecimal integers. It may also include a character from a to f or A to F that represents numbers from 10 to 15, respectively. For example, 0xd, 0xf,
  • Binary Integer: Base 2, whose digits consists of the numbers 0 and 1 (you can create binary literals in Java SE 7 and later). Prefix 0b represents the Binary system. For example, 0b11010.

Real Literals

The numbers that contain fractional parts are known as real literals. We can also represent real literals in exponent form. For example, 879.90, 99E-3, etc.

Backslash Literals

Java supports some special backslash character literals known as backslash literals. They are used in formatted output. For example:

\n: It is used for a new line

\t: It is used for horizontal tab

\b: It is used for blank space

\v: It is used for vertical tab

\a: It is used for a small beep

\r: It is used for carriage return

\': It is used for a single quote

\": It is used for double quotes

Character Literals

A character literal is expressed as a character or an escape sequence, enclosed in a single quote ('') mark. It is always a type of char. For example, 'a', '%', '\u000d', etc.

String Literals

String literal is a sequence of characters that is enclosed between double quotes ("") marks. It may be alphabet, numbers, special characters, blank space, etc. For example, "Jack", "12345", "\n", etc.

Floating Point Literals

The vales that contain decimal are floating literals. In Java, float and double primitive types fall into floating-point literals. Keep in mind while dealing with floating-point literals.

  • Floating-point literals for float type end with F or f. For example, 6f, 8.354F, etc. It is a 32-bit float literal.
  • Floating-point literals for double type end with D or d. It is optional to write D or d. For example, 6d, 8.354D, etc. It is a 64-bit double literal.
  • It can also be represented in the form of the exponent.

Floating:

Decimal:

Decimal in Exponent form:

Boolean Literals

Boolean literals are the value that is either true or false. It may also have values 0 and 1. For example, true, 0, etc.

Null Literals

Null literal is often used in programs as a marker to indicate that reference type object is unavailable. The value null may be assigned to any variable, except variables of primitive types.

Class Literals

Class literal formed by taking a type name and appending .class extension. For example, Scanner.class. It refers to the object (of type Class) that represents the type itself.

Invalid Literals

There is some invalid declaration of literals.

Restrictions to Use Underscore (_)

  • It can be used at the beginning, at the end, and in-between of a number.
  • It can be adjacent to a decimal point in a floating-point literal.
  • Also, can be used prior to an F or L suffix.
  • In positions where a string of digits is expected.

Why use literals?

To avoid defining the constant somewhere and making up a label for it. Instead, to write the value of a constant operand as a part of the instruction.

How to use literals?

A literal in Java can be identified with the prefix =, followed by a specific value.

Let's create a Java program and use above discussed literals.

LiteralsExample.java

Output:

987
4534.99
19765.567
2020
26
p
Java
true
55
null
!
	backslash literal
?





Latest Courses