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
1int decimal = 100;           // base 10 (normal)
2int binary  = 0b1100100;     // base 2  (0b prefix)
3int octal   = 0144;          // base 8  (0 prefix)
4int hex     = 0x64;          // base 16 (0x prefix)
5int readable = 1_00_000;     // underscore for readability (Java 7+)

2️⃣ Floating-Point Literals

java
QUBITS OF DPK
1double price = 99.99;        // double by default
2float temp   = 36.6f;        // f suffix for float
3double sci   = 1.5e3;        // scientific: 1500.0

3️⃣ Character Literals

java
QUBITS OF DPK
1char grade   = 'A';
2char unicode = '\u0041';     // Unicode for 'A'
3char newline = '\n';         // escape sequence

4️⃣ String Literals

java
QUBITS OF DPK
1String name  = "Deepak";    // stored in String Pool
2String empty = "";           // empty string

5️⃣ Boolean Literals

java
QUBITS OF DPK
1boolean active = true;       // lowercase only!
2boolean done   = false;

6️⃣ Null Literal

java
QUBITS OF DPK
1String name = null;          // only for reference types

️ Interview Traps

java
QUBITS OF DPK
1// Trap 1 — Underscore rules
2int wrong1 = _100;      // ❌ cannot start with underscore
3int wrong2 = 100_;      // ❌ cannot end with underscore
4int ok     = 1_00_000;  // ✅
5
6// Trap 2 — Boolean case
7boolean flag = True;    // ❌ capital T = compile error
8boolean flag = true;    // ✅ lowercase
9
10// Trap 3 — null only for objects
11int x = null;           // ❌ primitives can't be null
12String s = null;        // ✅

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.