In the previous topics, we learned that variables are used to store data. For example:
int age = 25;
String name = "John";
Here, age and name are variables. But what about the actual values 25 and "John"? These values written directly inside our program are called literals.
So, in simple words, a literal is a fixed value written directly in the source code.
For example:
int age = 25;
double price = 99.99;
char grade = 'A';
boolean isStudent = true;
String name = "John";
Here, 25, 99.99, 'A', true, and "John" are all literals.
Integer Literals
An integer literal is a whole number written directly in the program. It does not contain a decimal point.
For example:
int age = 25;
int score = 100;
int temperature = -5;
Here, 25, 100, and -5 are integer values written directly in the code.
Java also allows integer values to be written using different number systems, such as decimal, binary, octal, and hexadecimal.
int decimal = 10;
int binary = 0b1010;
int octal = 012;
int hexadecimal = 0xA;
All four of these represent the value 10, just written in different number systems. As a beginner, you will mostly use normal decimal numbers.
For very large long values, we commonly add L at the end:
long population = 8000000000L;
Floating-Point Literals
Numbers containing a decimal point are called floating-point literals.
For example:
double price = 49.99;
double height = 5.9;
By default, Java treats decimal literals as double.
If we want to use a decimal value with a float, we add f or F at the end:
float temperature = 36.5f;
Without the f, Java treats 36.5 as a double.
Character Literals
A character literal represents a single character and is written inside single quotes ' '.
For example:
char grade = 'A';
char symbol = '$';
char number = '5';
Each of these contains only one character.
This is valid:
char grade = 'A';
But this is not:
char grade = 'AB'; // Error
A char can store only one character.
String Literals
A String literal is a sequence of characters written inside double quotes " ".
For example:
String name = "John";
String city = "London";
String message = "Welcome to Java!";
Here, "John", "London", and "Welcome to Java!" are String literals.
Be careful with the quotes. A single character normally uses single quotes:
char grade = 'A';
While text uses double quotes:
String name = "John";
This difference is important in Java.
Boolean Literals
Boolean literals are the simplest because Java has only two of them:
true
false
They are used with boolean values:
boolean isLoggedIn = true;
boolean hasPermission = false;
Notice that true and false are written without quotation marks. Writing "true" would make it text rather than a boolean value.
Null Literal
Java also has a special literal called null. It represents the absence of an object or reference value.
For example:
String name = null;
Here, name currently doesn't refer to a String object.
Don't worry too much about null right now. It will make much more sense when we learn about objects and references later.
Literals in a Simple Program
Let's look at different literals together:
class Main {
public static void main(String[] args) {
int age = 25;
double height = 5.9;
char grade = 'A';
boolean isStudent = true;
String name = "John";
System.out.println(name);
System.out.println(age);
System.out.println(height);
System.out.println(grade);
System.out.println(isStudent);
}
}
In this example, the values "John", 25, 5.9, 'A', and true are literals because we have written those values directly inside the program.
The easiest way to understand literals is to look at a variable like this:
int age = 25;
int is the data type, age is the variable name, and 25 is the literal value being stored in that variable.