Literals in Java
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.
Integer literals
Whole numbers like 42. Add an L (or l) suffix — 42L — to make it a long instead of the default int.
Floating-point literals
Decimal numbers like 3.14, which default to double unless suffixed with f or F to make them a float instead.
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.
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.
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.
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); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.