Cosmic Module
J
Qubits of DPK
March 14, 2026
Core Java
Layman Explanation
A literal is any fixed value you directly type into your code. It's not calculated, not fetched — it just IS. Like 25 in int age = 25; — 25 is a literal.
6 Types of Literals
1️⃣ Integer Literals
java
QUBITS OF DPK
2️⃣ Floating-Point Literals
java
QUBITS OF DPK
3️⃣ Character Literals
java
QUBITS OF DPK
4️⃣ String Literals
java
QUBITS OF DPK
5️⃣ Boolean Literals
java
QUBITS OF DPK
6️⃣ Null Literal
java
QUBITS OF DPK
️ Interview Traps
java
QUBITS OF DPK
Interview Answer
"A literal is a fixed, hardcoded value directly written in code. Java has 6 types — integer (supports decimal, binary with 0b, octal with 0, hex with 0x, and underscore separators since Java 7), floating-point (double default, float needs f suffix), character (single quotes, supports Unicode escape), string (double quotes, stored in String Pool), boolean (only lowercase true/false), and null (only for reference types)."
Interview Questions & MAANG-Level Answers
Q1. What is a literal in Java?
A literal is any fixed, hardcoded value directly written in code that doesn't need computation. In int age = 25, the value 25 is an integer literal. In String name = "Deepak", the value "Deepak" is a String literal. Literals are the actual data values you embed directly in source code.
Q2. How do you write a binary literal in Java?
Use the 0b or 0B prefix. Example: int five = 0b101 (binary 101 = decimal 5). int hundred = 0b1100100 (binary = decimal 100). This was introduced in Java 7 and is useful when working with bit manipulation, flags, or low-level binary data.
Q3. What is the purpose of underscore in integer literals?
Also introduced in Java 7, underscores improve readability of large numbers without affecting value. int million = 1_000_000 is identical to int million = 1000000 but far more readable. Rules: cannot be at start, end, next to decimal point, or next to 0x/0b prefix. Common use: phone numbers, credit card numbers, large financial values.
Q4. Can null be assigned to a primitive type?
No. null is only valid for reference types (objects, arrays, String). Primitives always hold a value — they cannot be absent. int x = null is a compile error. If you need a nullable numeric value (e.g., optional field in a database entity), use the wrapper class: Integer x = null is valid since Integer is an object.
Q5. What is the difference between 'A' and "A" in Java?
'A' is a char literal — single character, uses single quotes, stored as Unicode value 65 (2 bytes). "A" is a String literal — an object of class String, uses double quotes, stored in String Pool in heap. They are completely different types: char c = 'A' vs String s = "A". You cannot assign "A" to a char variable or 'A' to a String variable directly.