Learn Java
import com.learnjava.gettingstarted.Literals;

Literals in Java

@Concept

A literal is a fixed value written directly into source code, as opposed to a variable, which is just a name that refers to a value. Java has integer literals (42), floating-point literals (3.14), character literals ('A'), string literals ("hello"), boolean literals (true and false), and the null literal.

A few suffixes and prefixes disambiguate exactly which type a literal is: an L makes an integer a long instead of the default int, an f makes a decimal a float instead of the default double, 0x marks a hexadecimal integer, 0b marks a binary one, and underscores can be dropped anywhere inside a numeric literal purely to help a human read it, since the compiler ignores them entirely.

@HowItWorks
1

Integer literals

Whole numbers like 42. Add an L (or l) suffix — 42L — to make it a long instead of the default int.

2

Floating-point literals

Decimal numbers like 3.14, which default to double unless suffixed with f or F to make them a float instead.

3

Character and string literals

A single character in single quotes, like 'A', is a char. Text in double quotes, like "hello", is a String — the quote style is not interchangeable in Java.

4

Boolean and null literals

Exactly two boolean values exist, true and false, plus null, meaning "no object" — valid only for reference types, never for primitives.

5

Alternate bases and readability underscores

0x1F (hex), 0b1010 (binary), and 017 (octal) all denote integers written in a different base, and underscores like 1_000_000 can go anywhere between digits purely to help a human read a long number.

@Example
public class Literals {
public static void main(String[] args) {
int hex = 0x2A;
long big = 3_000_000_000L;
double pi = 3.14159;
char grade = 'A';
boolean passed = true;
System.out.println(hex + ", " + big + ", " + pi);
System.out.println(grade + " grade, passed=" + passed);
}
}
Output
42, 3000000000, 3.14159
A grade, passed=true
10x2A is a hexadecimal literal — the compiler stores it as the plain integer 42, which is what actually prints.
23_000_000_000L needs the L suffix because that value is too large for a default int, which tops out around 2.1 billion; the underscores are purely for readability and ignored by the compiler.
3pi takes the default double type since 3.14159 has no f suffix attached to it.
4The two println calls each run once, producing exactly two lines of output.
@UseCases
Reading a hex or binary constant in networking code or bit-flag logic without tripping over its base
Knowing why 3.14 assigned to a float variable fails to compile without an f suffix
Writing large numeric constants — byte thresholds, timestamps — with underscores for readability
Telling apart a char like 'A' from a one-letter String like "A" when debugging a type error

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.