Literals in Java

Literals in Java is a source code representation of a fixed value. They are represented directly in the code and are used to represent values such as integers, floating-point numbers, characters, and strings.

The following literals are available in Java:

  • Integral Literals / Integer Literals
  • Floating Point Literals
  • Boolean Literals
  • String Literals

Integral Literals / Integer Literals

These can be written in decimal (base 10), hexadecimal (base 16) or octal (base 8) notation. The Integer Literals are:

  • long
  • int
  • short
  • byte
  • char
long number = 12345;
int n = 1223;
short s = 12;
byte b = 13;
char c = 'A';

In the above example all the values 12345, 1223, 12, 13 and A are integer literals.

Floating Point Literals

These represent decimal values and are written with a decimal point and an optional exponent. Following are the floating point literals:

  • float
  • double
float f = 123.45f;
double d = 12345.6789;

Any values assigned to these data types are literals. Hence, the value 123.45f and 12345.6789 are floating point literals.

Boolean Literals

  • boolean

The value assigned to boolean variables is literals. Hence, the value true and false boolean literals.

String Literals

The value assigned to the String type is all literal. These are sequences of characters enclosed in double quotes.

  • String

The value inside double quote ” are string literals. For example:

"5", "true", "Radha Krishnan", "123.45", "1234.4567"

Null Literals

The null literal is the special literal null that represents a null reference, which is a reference that does not refer to any object.

Example:

Student student = null;

In the following integral literal it is very hard to read the value.

int number = 986456354;

But, after JDK version 1.7 Java has started to support underscore ‘_’ inside integral literals so that we can separate literals and easily read the representation of the word by the developer/programmer. If we try to read the value it is very hard but in the following example, we can easily read it like Ninety-Eight Crore Sixty Four Lakhs Fifty Six Thousands Three Hundred Fifty Four.

int number = 98_64_56_354;

During the compile time, the compiler automatically removes these extra characters inside the integral literal and keeps only the number so that it does not affect the actual value.

Frequently Asked Questions

What are literals in Java?

In Java, literals are fixed values used to represent constants. They can be of various types, such as numbers, characters, strings, and boolean values.

What are some common types of literals in Java?

Common literals in Java include integer literals (e.g., 42), floating-point literals (e.g., 3.14), character literals (e.g., ‘A’), string literals (e.g., “Hello, World!”), and boolean literals (e.g., true or false).

How are numeric literals in Java categorized?

Numeric literals can be categorized into integer literals (integers without a decimal point) and floating-point literals (numbers with a decimal point or in scientific notation).

Can I use special characters in string literals in Java?

Yes, you can include special characters and escape sequences in string literals, such as newline (\n), tab (\t), and backslash (\).

What is the significance of the ‘L’ or ‘D’ suffix in numeric literals (e.g., 100L or 3.14D)?

The ‘L’ suffix indicates a long integer literal, and the ‘D’ suffix indicates a double-precision floating-point literal, explicitly specifying the data type for the literal value.

Sharing Is Caring:
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments