Imagine you walk into a shop and say, “I want 2 pencils and 1 notebook.” Here, 2 and 1 are fixed values as they don’t change. In the same way, in programming we also deal with fixed values. These fixed values are called literals.
What are Literals in Java?
Literals are the constant values that are directly written in the program. They are not variables, but the actual values we assign to variables. A literal can be a number, a character, a string or any other predefined value. For example:
int age = 15;
Here, 15 is a literal and age is the variable.
Types of Literals in Java
1. Integer Literals:
Integer literals are whole numbers without any fractional or decimal part. Example:
int a = 100;
int b = -45;
Note: Integer literals can be written in:
- Decimal Integer: Decimal integers use a base ten and digits ranging from 0 to 9. They can have a negative (-) or a positive (+), but non-digit characters or commas aren’t allowed between characters. Example: 2022, +42, -68.
- Octal Integer: Octal integers use a base eight and digits ranging from 0 to 7. Octal integers always begin with a “0.” Example: 007, 0295.
- Hexa-Decimal: Hexa-decimal integers work with a base 16 and use digits from 0 to 9 and the characters of A through F. The characters are case-sensitive and represent a 10 to 15 numerical range. Example: 0xf, 0xe.
- Binary Integer: Binary integers uses a base two, consisting of the digits “0” and “1.” The prefix “0b” or “0B” represents the Binary system. Example: 0b11011.
2. Floating-Point Literals:
Floating-point literals are numbers with a decimal point or written in exponential (scientific) notation. Example:
double pi = 3.14; // decimal form
float rate = 2.5f; // 'f' is used for float
double exp = 1.2e3; // scientific notation = 1.2 × 10³ = 1200.0
3. Character Literals:
A character literal represents a single character enclosed in single quotes (‘ ‘). Example:
char grade = 'A';
char digit = '7';
char symbol = '$';
Special characters can also be written using escape sequences (like ‘\n’, ‘\t’, ‘\”, ‘\”‘, ‘\\’).
4. String Literals:
A string literal is a sequence of characters enclosed within double quotes (” “). Example:
String name = "Java";
String msg = "Welcome to Programming";
Unlike character literals, string literals can hold multiple characters.
5. Boolean Literals:
Boolean literals can have only two values- true or false. Example:
boolean isJavaFun = true;
boolean isEarthFlat = false;
6. Null Literal:
The null literal represents the absence of a value. It is used with reference types (objects, arrays etc.). Example:
String str = null;
int[] numbers = null;
How Java Literals Work?
When you write a literal in your code, Java directly understands and stores it as a value. For example:
String name = "Rahul";
boolean isStudent = true;
double pi = 3.14159;
Java uses these literals to initialize variables and make the program meaningful.
Final Thought on Literals
Literals in Java are the fixed values that give life to variables and meaning to your code. Just like words in a story cannot change on their own, literals are constant and stay the same throughout the program.
If you’re thinking of learning program
Leave a Comment