Literals
Literals are fixed values assigned directly to variables in a Java program. They represent constant data written in the source code and play a crucial role in data initialization, readability, and correctness.
Understanding literals is important for syntax clarity, type selection, and interviews.
What Are Literals?
- Constant values written directly in code
- Assigned to variables
- Have a specific data type
- Stored in memory at runtime
Example:
int x = 10; // 10 is a literal
String s = "Java"; // "Java" is a literal
Types of Literals in Java
Java supports the following types of literals:
- Integer Literals
- Floating-Point Literals
- Character Literals
- String Literals
- Boolean Literals
- Null Literal
1. Integer Literals
Used to represent whole numbers.
a) Decimal Literals (Base 10)
int a = 100;
b) Binary Literals (Base 2 – Java 7+)
Prefix: 0b or 0B
int b = 0b1010; // 10
c) Octal Literals (Base 8)
Prefix: 0
int c = 012; // 10
d) Hexadecimal Literals (Base 16)
Prefix: 0x or 0X
int d = 0xA; // 10
Integer Literal Default Type
- Default type is int
- For long, suffix L or l is mandatory
long num = 10000000000L;
2. Floating-Point Literals
Used to represent decimal numbers.
a) Double Literals (Default)
double pi = 3.14;
b) Float Literals
- Must end with f or F
float price = 99.99f;
Scientific Notation
double value = 1.23e3; // 1230.0
3. Character Literals
Used to represent a single character.
Valid Character Literals
char ch1 = 'A';
char ch2 = '9';
char ch3 = '@';
Unicode Character Literals
char ch = '\u0041'; // 'A'
Escape Sequences
char newLine = '\n';
char tab = '\t';
char quote = '\'';
4. String Literals
- Sequence of characters enclosed in double quotes
- Stored in String Constant Pool
- Immutable in nature
String s = "SoftwareTips4U";
Why it matters: Improves memory efficiency by reusing string literals.
5. Boolean Literals
Only two possible values:
boolean isValid = true;
boolean isDone = false;
⚠️ Java does not support 0 or 1 for boolean.
6. Null Literal
- Represents no object reference
- Applicable only to non-primitive types
String name = null;
Why it matters: Used to indicate absence of object.
Underscore in Numeric Literals (Java 7+)
- Improves readability
- Ignored by compiler
int amount = 1_000_000;
long cardNo = 1234_5678_9012_3456L;
❌ Invalid usage:
int x = _100; // ❌
int y = 100_; // ❌
Summary Table
| Literal Type | Example | Notes |
|---|---|---|
| Integer | 10, 0xA | Default = int |
| Floating | 3.14, 2.5f | Default = double |
| Character | 'A', '\n' | Single character |
| String | "Java" | Immutable |
| Boolean | true, false | Only two values |
| Null | null | For objects only |
Common Mistakes by Beginners
- Forgetting L for long literals
- Forgetting f for float literals
- Using double quotes for char
- Using single quotes for String
- Assigning null to primitive types
Interview-Ready Answers
Short Answer
Literals are fixed constant values assigned directly to variables in Java.
Detailed Answer
Literals in Java represent constant values such as integers, floating-point numbers, characters, strings, booleans, and null. They are directly written in the source code and help initialize variables with specific data.
Key Takeaway
Literals are the simplest form of data in Java. Correct usage ensures readable code, proper typing, and fewer runtime errors.