Literals
Literals
A school program needs reliable data and clear rules. A literal writes a value directly in source code. Numbers, quoted text and true or false have different types, so a student’s name and mark need different representations.
Understand
A literal writes a value directly in source code.
Apply
Trace the Java example, predict its output and explain the result before running it.
Avoid mistakes
A quoted number is text. A character literal uses single quotes; a String uses double quotes.
Introduction to Java Literals
In Java, literals are the most direct way to represent fixed values in your code. They anchor your program’s meaning with concrete data—like numbers, text, or true/false flags—helping the compiler and readers alike understand exactly what you intend.
1. Integer Literals
Integers can be expressed in four bases:
- Decimal (base 10): the default, e.g.
42 - Binary (base 2): prefix with
0bor0B, e.g.0b101010 - Octal (base 8): prefix with
0, e.g.052 - Hexadecimal (base 16): prefix with
0xor0X, e.g.0x2A
public class IntegerLiteralsExample {
public static void main(String[] args) {
int dec = 42;
int bin = 0b101010;
int oct = 052;
int hex = 0x2A;
System.out.println(dec);
System.out.println(bin);
System.out.println(oct);
System.out.println(hex);
}
}
42
42
42
42
All four declarations yield the same numeric value, showcasing Java’s flexible syntax.
2. Floating-Point Literals
By default, decimal fractions are double. To specify a float, append f or F:
public class FloatLiteralsExample {
public static void main(String[] args) {
double d1 = 3.14159;
float f1 = 3.14159F;
System.out.println(d1);
System.out.println(f1);
}
}
3.14159
3.14159
Note that without F, the compiler treats the literal as double and will flag an error if assigned to float.
3. Boolean Literals
Boolean literals are straightforward: true and false. They power conditional logic:
public class BooleanLiteralsExample {
public static void main(String[] args) {
boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun);
System.out.println(isFishTasty);
}
}
true
false
4. Character Literals
Character literals represent single UTF-16 code units, enclosed in single quotes:
'A'for letters'7'for digits- Escape sequences like
' ','\u03A9'(Ω)
public class CharLiteralsExample {
public static void main(String[] args) {
char letter = 'J';
char digit = '9';
char newline = '\n';
char omega = '\u03A9';
System.out.println(letter);
System.out.println(digit);
System.out.println(newline);
System.out.println(omega);
}
}
J
9
Ω
5. String Literals
Strings are sequences of characters wrapped in double quotes. Java 15+ also supports text blocks using triple quotes:
public class StringLiteralsExample {
public static void main(String[] args) {
String greeting = "Hello, Java!";
String poem = """
Roses are red,
Violets are blue,
Java is great,
And so are you!
""";
System.out.println(greeting);
System.out.println(poem);
}
}
Hello, Java!
Roses are red,
Violets are blue,
Java is great,
And so are you!
6. Null Literal
The special literal null indicates the absence of an object reference:
public class NullLiteralExample {
public static void main(String[] args) {
String text = null;
if (text == null) {
System.out.println("No text assigned yet.");
}
}
}
No text assigned yet.
Use null carefully to avoid NullPointerException.
Important boundaries and mistakes
A quoted number is text. A character literal uses single quotes; a String uses double quotes.
Try the school example
Before running this Java program, write down each line you expect to see. Identify the value, branch, object or exception responsible for each line. Then explain whether the following assumption is valid: A quoted number is text.
Java 17 · Main.java · complete practice program
public class Main {
public static void main(String[] args) {
String name = "Diya";
int marks = 72;
System.out.println(name + ": " + marks);
}
}
Now change one thing: Replace the name Diya with Saira. Predict the new behavior before checking the worked variation.
Worked answer and reasoning
Java 17 · Main.java · source repeated for this answer
public class Main {
public static void main(String[] args) {
String name = "Diya";
int marks = 72;
System.out.println(name + ": " + marks);
}
}
Expected output
Diya: 72A literal writes a value directly in source code. Numbers, quoted text and true or false have different types, so a student’s name and mark need different representations.
A quoted number is text. A character literal uses single quotes; a String uses double quotes.
Run with javac Main.java, then java Main. The declaration of Main names the class; main starts the example, and println writes a complete output line.
Worked variation: Replace the name Diya with Saira. The numeric mark remains 72 and the report label changes.
Check your understanding
What to remember
A literal writes a value directly in source code. Numbers, quoted text and true or false have different types, so a student’s name and mark need different representations. A quoted number is text. A character literal uses single quotes; a String uses double quotes.
Related explanations: Java Variables, Java If Statement - Syntax and Examples, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide and Java 17 language specification.
Reaching the recap and completing the quizzes are separate milestones.