Literals and escapes
What you will learn
A literal writes a value directly in source code. Java supports integer, floating-point, boolean, character, string and null literals. Escape sequences represent characters that would otherwise be awkward to write in quoted text.
public class Main { public static void main(String[] args) { System.out.println(10); System.out.println(0b1010); System.out.println(0xA); }}Expected output:
101010The different literal bases denote the same integer.
How it works
Use 0b1010 for binary, 0xA for hexadecimal, and 10 for decimal. A leading zero on an integer literal can indicate octal. Numeric underscores improve readability but have placement restrictions. In a string, \n represents a newline, \t a tab, \" a double quote and \\ a backslash.
public class Main { public static void main(String[] args) { System.out.println("Shelf: \"A\"\nReady"); }}Expected output:
Shelf: "A"ReadyThe escaped quotation marks appear as text; the newline separates the lines.
Common mistakes
Do not write leading zeros merely to align decimal values: 010 means eight in octal, not ten. Use plain decimal or a string for formatting an identifier such as a room code.
Try it yourself
Print an item code containing literal quotes, then print the value 15 in decimal and hexadecimal notation.
These are local Java practice tasks, not automatically graded ByteMind exercises. Use a patched JDK; no network or third-party dependency is needed.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Literals and escapes — knowledge check
Check your understanding of literals and escapes.
What is the decimal value of 0xA?
What does the escape sequence \n represent in a string?