1. The Hook (The "Byte-Sized" Intro)
- In a Nutshell: Literals = fixed values written directly in code.
- Integer literals: decimal (42), binary (0b101010), octal (0o52), hex (0x2A).
- Floating-point: double default (3.14), float needs 'f' (3.14f), scientific (1.23e5).
- Character: single quotes ('A'), escape sequences ('\n', '\t'), Unicode ('\u0041').
- String: double quotes ("Hello"), text blocks ("""multi-line""").
- Boolean: true/false. null: represents no object.
- Underscores: 1_000_000 for readability.
- Golden rule: Choose right literal type to match variable type!
Think of writing. Literals = actual words/numbers on page. Integer formats = different number systems (Roman numerals, Arabic). String literals = quoted text. Escape sequences = special symbols (newline, tab). Underscores = commas in large numbers (1,000,000)!
2. Conceptual Clarity (The "Simple" Tier)
💡 The Analogy
- Literal: Exact value written down (not calculated)
- Integer literal: Number on paper (42)
- String literal: Text in quotes ("Hello")
- null: Empty box label
3. Technical Mastery (The "Deep Dive")
java
// ===========================================
// 1. INTEGER LITERALS
// ===========================================
// Decimal (base 10) - DEFAULT
int decimal = 42;
int million = 1_000_000; // ✅ Underscores for readability!
// Binary (base 2) - Prefix: 0b or 0B
int binary = 0b101010; // 42 in binary
int flags = 0b1111_0000; // Grouped for readability
// Octal (base 8) - Prefix: 0
int octal = 052; // 42 in octal (rarely used)
// Hexadecimal (base 16) - Prefix: 0x or 0X
int hex = 0x2A; // 42 in hex
int color = 0xFF5733; // Common in graphics/colors
// All represent same value:
System.out.println(42); // 42
System.out.println(0b101010); // 42
System.out.println(052); // 42
System.out.println(0x2A); // 42
// Long literals (add L or l suffix)
long bigNumber = 10_000_000_000L; // ✅ Use L (not l, looks like 1)
long hexLong = 0xFFFFFFFFL;
// ===========================================
// 2. FLOATING-POINT LITERALS
// ===========================================
// double (DEFAULT for decimals)
double price = 19.99;
double pi = 3.14159;
double large = 1.23456789012345; // ~15 decimal digits
// float (add f or F suffix)
float temperature = 36.6f; // ✅ Must use f suffix!
float percentage = 85.5F;
// ❌ Missing f suffix
float value = 10.5; // ❌ Compile error! (10.5 is double by default)
// Scientific notation
double scientific = 1.23e5; // 1.23 × 10^5 = 123,000
double smallNumber = 3.14e-10; // 0.000000000314
float floatSci = 1.5e3f; // 1500.0f
// ===========================================
// 3. CHARACTER LITERALS
// ===========================================
// Basic characters (single quotes!)
char letter = 'A';
char digit = '7'; // Character '7', not number 7
char symbol = '$';
char space = ' ';
// ❌ Double quotes (that's a String!)
char wrong = "A"; // ❌ Compile error!
// Escape sequences
char newline = '\n'; // Newline
char tab = '\t'; // Tab
char backslash = '\\'; // Backslash
char singleQuote = '\''; // Single quote
char doubleQuote = '\"'; // Double quote
// Unicode characters
char unicode = '\u0041'; // 'A'
char heart = '\u2764'; // ❤
char euro = '\u20AC'; // €
// Numeric char values (ASCII/Unicode code)
char asciiA = 65; // 'A' (ASCII 65)
char nextChar = (char)(asciiA + 1); // 'B'
// ===========================================
// 4. STRING LITERALS
// ===========================================
// Basic strings (double quotes!)
String message = "Hello, World!";
String empty = ""; // Empty string
String name = "Alice";
// Escape sequences in strings
String multiLine = "Line 1\nLine 2\nLine 3";
String tabbed = "Name:\tAlice";
String quoted = "She said, \"Hello!\"";
String path = "C:\\Users\\Alice\\Documents"; // Windows path
// Concatenation
String fullName = "Alice" + " " + "Smith";
String greeting = "Hello, " + name + "!";
// Text Blocks (Java 13+) - Multi-line strings
String json = """
{
"name": "Alice",
"age": 25,
"city": "New York"
}
""";
String html = """
<html>
<body>
<h1>Welcome!</h1>
</body>
</html>
""";
// ===========================================
// 5. BOOLEAN LITERALS
// ===========================================
// Only two values: true and false
boolean isActive = true;
boolean hasPermission = false;
boolean isValid = (10 > 5); // Result is boolean literal
// ❌ NOT boolean literals (these are numbers!)
boolean wrong1 = 1; // ❌ Compile error!
boolean wrong2 = "true"; // ❌ Compile error!
// ===========================================
// 6. NULL LITERAL
// ===========================================
// null = absence of object (reference types only)
String name = null;
Student student = null;
int[] numbers = null;
// ❌ Cannot use with primitives
int value = null; // ❌ Compile error!
boolean flag = null; // ❌ Compile error!
// Checking for null
if (name == null) {
System.out.println("Name is not set");
}
// ===========================================
// 7. UNDERSCORE IN LITERALS (Readability)
// ===========================================
// ✅ GOOD: Use underscores for readability
int million = 1_000_000;
long trillion = 1_000_000_000_000L;
double pi = 3.141_592_653_59;
int creditCard = 1234_5678_9012_3456;
int binary = 0b1111_0000_1010_1100;
// ❌ BAD: Hard to read
int million = 1000000;
long trillion = 1000000000000L;
// Rules for underscores:
// ✅ Can appear between digits
// ❌ Cannot appear at beginning or end
// ❌ Cannot appear next to decimal point
int valid = 1_000; // ✅
int invalid1 = _1000; // ❌
int invalid2 = 1000_; // ❌
double invalid3 = 3_.14; // ❌
// ===========================================
// 8. TYPE MATCHING
// ===========================================
// ✅ Match literal to variable type
byte b = 127; // ✅ Fits in byte
int i = 1_000_000; // ✅ int literal
long l = 10_000_000_000L; // ✅ long literal (needs L)
float f = 3.14f; // ✅ float literal (needs f)
double d = 3.14; // ✅ double literal (default)
char c = 'A'; // ✅ char literal (single quotes)
String s = "Hello"; // ✅ String literal (double quotes)
boolean bool = true; // ✅ boolean literal
// ❌ Type mismatches
byte b2 = 128; // ❌ Out of range (-128 to 127)
float f2 = 3.14; // ❌ Missing f (3.14 is double)
char c2 = "A"; // ❌ Wrong quotes (char uses single)5. The Comparison & Decision Layer
| Literal Type | Syntax | Example |
|---|---|---|
| Decimal | Plain number | 42 |
| Binary | 0b prefix | 0b101010 |
| Octal | 0 prefix | 052 |
| Hexadecimal | 0x prefix | 0x2A |
| Long | L suffix | 10_000L |
| Float | f suffix | 3.14f |
| Double | Plain decimal | 3.14 |
| Character | Single quotes | 'A' |
| String | Double quotes | "Hello" |
| Boolean | true/false | true |
| Null | null | null |
6. The "Interview Corner" (The Edge)
The "Killer" Interview Question:
"Why does float f = 3.14; not compile?"
Answer: Because 3.14 is a double literal by default!
java
float f = 3.14; // ❌ Type mismatch: double → float (narrowing)
float f = 3.14f; // ✅ float literal
float f = (float)3.14; // ✅ Explicit cast
// Similarly:
long l = 10_000_000_000; // ❌ Integer literal too large
long l = 10_000_000_000L; // ✅ long literalPro-Tips:
- Use underscores for readability:
java
// ❌ Hard to read
int population = 8000000000;
// ✅ Easy to read
int population = 8_000_000_000;
// ✅ Group by purpose
int creditCard = 1234_5678_9012_3456;
int ssn = 123_45_6789;- Hexadecimal for colors/flags:
java
// RGB colors
int red = 0xFF0000;
int green = 0x00FF00;
int blue = 0x0000FF;
int white = 0xFFFFFF;
// Bit flags
int READ = 0b0001;
int WRITE = 0b0010;
int EXECUTE = 0b0100;
int ALL = 0b0111; // READ | WRITE | EXECUTE