ReviseAlgo Logo

Basic Syntax

Literals

Represent static data values: integral representations, floating-point suffix tags, character sets, and modern text blocks.

Interview: Expect questions on numeric prefix markers, literal suffix tags (L, f, d), and text block formatting whitespace rules.

Last Updated: June 13, 2026 10 min read

A literal is a source code representation of a fixed data value. In Java, literals are used to assign static values to variables directly, covering Integer, Floating-Point, Character, String, Boolean, and Null literals.

Core Idea

Literals represent fixed values directly. Suffixes (like L or f) tell the compiler how to size the value in memory.

Why It Matters

Omitting a suffix (like f on decimal literals) will cause compile-time type mismatch errors because decimals default to 64-bit doubles.

Interview Lens

Expect questions on binary/hex prefix rules, underscores in numbers, and modern Java Text Block indentations.

Numeric Literals

1. Integer Radixes

Java supports four numerical base systems:

  • Decimal (Base 10): Standard numbers (e.g. 50).
  • Binary (Base 2): Prefixed with 0b or 0B (e.g. 0b110010).
  • Octal (Base 8): Prefixed with a leading 0 (e.g. 062).
  • Hexadecimal (Base 16): Prefixed with 0x or 0X (e.g. 0x32).

2. Type Suffix Rules

  • Long Literals: Suffix with L or l (e.g. 3000000000L). Always use uppercase L because lowercase l looks like the number 1.
  • Floating-Point Defaults: Any decimal literal (e.g. 3.14) is treated as a double by default. To assign it to a float, you must append the f or F suffix (e.g. 3.14f).

3. Underscores in Numeric Literals

Since Java 7, you can place underscores between digits to make large numbers easier to read (e.g. 1_000_000). Underscores cannot be placed adjacent to decimal points or prefix/suffix characters.

Modern Text Blocks (Java 15)

Text blocks represent multi-line strings, declared using triple double quotes ("""). They preserve whitespace and newlines without requiring escape sequences (like ), and handle internal double quotes automatically.

Common Pitfalls

  • The Octal Trap: Writing a number with a leading zero (like int val = 010;) changes the base to octal. This compiles to the decimal value 8, not 10. Avoid leading zeros on standard integers.
  • Omitted Float Suffix: Writing float f = 5.6; causes a compilation error because double values cannot be implicitly cast to floats. Use float f = 5.6f; instead.

Best Practices

  • Use Uppercase L: Always write long literals with an uppercase L (e.g. 5_000_000_000L) to make the code easier to read.
  • Use Text Blocks for Structured Content: Use text blocks when writing multi-line strings (like SQL queries, JSON payloads, or HTML snippets) to improve readability.

Interview-Relevant Information

Q1: Why does long val = 3000000000; fail to compile?
Answer: In Java, integer literals are treated as 32-bit ints by default. Since 3 billion exceeds the maximum value of an int (2.14 billion), compilation fails before the value can be assigned. To fix this, append an L suffix (e.g. 3000000000L) to treat the literal as a 64-bit long.

Q2: How are indentations handled in modern Java Text Blocks?
Answer: The compiler calculates the common minimum indentation (relative to the closing triple quotes """) and strips this shared leading whitespace from each line, preserving only relative indentation.

Quick Checklist

Can you write a number in binary, octal, and hex, explain the octal trap, and write a multi-line string using a Text Block? If yes, you understand Java literals.

Use Cases

Representing large constant numbers (like milliseconds in a day: 86_400_000L) clearly in codebases.

Formatting multi-line SQL queries or JSON mock payloads cleanly using text blocks.

Common Mistakes

Accidentally introducing octal values by writing standard numbers with leading zeros (e.g. writing 08 which is an invalid octal number and fails to compile).

Forgetting to suffix long or float variables, causing compilation failures.