Java literals synthetically represent boolean, character, string, numeric, and floating point data. The Java literals let us express particular values within a program. We can directly write these values in our code without requiring computation or evaluation. Java supports multiple literal types that will be discussed in this post along with practical examples.
What Are Literals in Java
A literal in Java is a fixed value directly written into the code and cannot be changed during program execution.
What Are Different Types of Literals in Java
Java supports the below-listed Types of “Literals” and these types are further classified into several subtypes:
- Integral Literals
- Floating-Point Literal
- Char or Character Literals
- String Literals
- Boolean Literals
- Null Literals
- Backslash Literals
- Invalid Literals
Let’s start with the integral literals.
1) Integral Literals in Java
In Java, we can have four types of integral literals/numbers: “Integer”, “long”, “short”, and “byte”. By default, each literal is of type int or integer, however, we can specify it explicitly as the long type using the “l” or “L” suffix. We can’t specify short or byte literals explicitly, however, we can do that indirectly. If we declare a variable of type byte and assign it with a value that lies within the range of byte, then the Java compiler treats it as a byte literal.
To specify these integral literals, opt for one of the following ways:
- Decimal Literals (Base 10)
- Octal Literals (Base 8)
- Hexa-Decimal Literals (Base 16)
- Binary Literals (Base 2)
Decimal Literals (Base 10)
The decimal literals are the most commonly used integral literals that allow digits from 0-9. These literals can be positive or negative. Here are some examples:
int number = 5;int number1 = -512;int number2 = 51214;
Octal Literals (Base 8)
Another way of specifying the integer literals is the Octal form that lets us specify digits between 0-7. However, it is important to note that an octal literal must be prefixed with a “0”, as shown in the following example:
int number = 0123
Hexa-Decimal Literals (Base 16)
We can also specify an integral literal in base 16, known as hexadecimal literals. We can specify digits between 0-9 and characters between “a-f” or “A-F”. Where the characters represent numbers from 10-15, i.e., a & A represent 10, b & B represent 11, and so on. The hexadecimal values/literals are prefixed with “0X” or “0x”. Here are some examples:
int number = 0X6;
int number1 = 0Xb;
Binary Literals (Base 2)
Java allows us to specify an integer in base, known as binary literal. The binary literals allow us to specify a digit between 0 and 1. A binary literal must be prefixed with 0B or 0b, as illustrated in the following examples:
int number = 0b1010;
int number1 = 0B1110;
2) Floating-point Literals in Java
The numbers that include a floating point or fractional part are known as the “floating-point” or “real” literals. We can specify a floating point value in decimal form or exponential notation. The floating-point literals are positive by default, however, to represent a negative value, we need to specify/use the “-” symbol. The valid digits to specify floating-point literals are 0-9. Here is an example:
double number = 654.321;float number = 654.321f;
Note: By default, a floating-point literal is of type double, so we can’t assign it to a float-type variable. However, we can use the suffix “f” or “F” to assign it to a float variable. We can’t specify a floating-point literal in octal or hexadecimal form.
3) Char or Character Literals in Java
A char/character literal in Java has a data type “char” and is expressed as a character or an escape sequence. We can specify a char literal in Java in the following ways:
Single Quote
We can assign a literal to a char data type by specifying a single character within single quotes. Here is an example:
char gender = 'M';
Char as Integral Literal
We can represent a character literal as an integral value, which indicates the Unicode value of the character. This integral value can be expressed/represented in Decimal, Hexadecimal, or Octal representation. However, it’s crucial to note that the valid range for this value is from 0 to 65535. Here is an example:
char x = 65;
Unicode Representation
In Java, we can specify a char literal in Unicode representation by using the escape sequence “\u” followed by a four-digit hexadecimal number, as illustrated below:
char x = '\u0065'; //it represent lowercase letter e
Escape Sequence
Each escape character can be represented as a char literal, as shown in the following example:
char x = '\t'; //here horizontal tab is specified as a char literal
4) String Literals in Java
A string literal in Java represents a sequence of characters specified within double quotations. This sequence may include alphabets, special symbols, numbers, blank spaces, etc. Here are some examples of String literals in Java:
String str = "javabeat";
String str = "java@beat";
String str = "12java123beat12";
5) Boolean Literals in Java
The Boolean literals in Java let us specify only one of two values: “true” or “false”. Here is an example:
boolean isAvailable = true; boolean isAvailable = false;
6) Null Literals in Java
The null keyword represents that a reference-type object isn’t available. It can be assigned to any variable except those of primitive types. Here is an example that demonstrates how to specify a null literal in Java:
String productDescription = null;
7) Backslash Literals in Java
A literal that is prefixed with a backslash is referred to as a backslash literal. These literals are illustrated in the following table:
Literal | Description |
---|---|
\t | It represents/adds a horizontal tab. |
\v | It represents/adds a vertical tab. |
\n | It shifts/moves the cursor to a new line. |
\’ | It is used to add single quotes. |
\” | It adds double quotes. |
\a | It adds a small beep. |
\b | It adds a blank space. |
\r | It is used for carriage return, which means it moves the cursor to the start of the current line without proceeding to the next line. |
8) Invalid Literals
Be careful while declaring and using literals in Java, otherwise, you may face unwanted results. Here are some examples of invalid declarations of Java Literals:
- Underscores are not allowed immediately before or after a decimal point. For example, “123_.456” and “123._456” are invalid literals.
- We can’t use underscores as the last character of a numeric literal like this “100_”.
- We can’t use an underscore immediately before or after the ‘0x’ or ‘0X’ prefix, like “0x_52” or “0X_44”.
All in all, Underscores have to be located within digits.
How Literals Work in Java
This practical example demonstrates how to use literals in Java:
public class Example {
public static void main(String[] args) {
//Integral Literals
int num = 172;
int hexaNum = 0x5e3;
int binaryNum = 0b1110;
int octalNum = 054;
//Floating-point Literals
double doubleNum = 654.321;
float floatNum = 654.321f;
//Char Literals
char gender = 'M';
char x = 65;
char y = '\u0065';
char z = '\t';
//String Literals
String str = "javabeat";
String str1 = "java@beat";
String str2 = "12java123beat12";
//Boolean Literals
boolean isAvailable = true;
//Null Literals
String productDescription = null;
System.out.println("Decimal Literals ==> " + num);
System.out.println("HexaDecimal Literals ==> " + hexaNum);
System.out.println("Binary Literals ==> " + binaryNum);
System.out.println("Octal Literals ==> " + octalNum);
System.out.println("Floating-point Literals ==> " + doubleNum);
System.out.println("Floating-point Literals ==> " + floatNum);
System.out.println("Char Literals ==> " + gender);
System.out.println("Integral Literal as Char Literal ==> " + x);
System.out.println("Char Literal Unicode Representation ==> " + y);
System.out.println("Char Literal Escape Sequence ==> " + z);
System.out.println("String Literal ==> " + str);
System.out.println("String Literal Containing Special Character ==> " + str1);
System.out.println("Alphanumeric String Literal ==> " + str2);
System.out.println("Boolean Literals ==> " + isAvailable);
System.out.println("Null Literals ==> " + productDescription);
}
}
The above code implements Integral, Floating-point, Char, String, Boolean, and Null Literals in Java:
That’s all about Literals in Java.
Final Thoughts
A literal in Java is a synthetic representation of a boolean, char, string, or numeric data. It is a way of expressing a specific value in a program, such as int age = 28; String name = “Joseph”. These values remain constant throughout the program.
Java supports several types of literals, such as “Integral”, “floating-point”, “boolean”, “char”, “string”, “backslash”, and “null” literals. This Java tutorial has demonstrated all these literal types with suitable examples.