Java Literals
Understanding Data Representations
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
0b
or0B
, e.g.0b101010
- Octal (base 8): prefix with
0
, e.g.052
- Hexadecimal (base 16): prefix with
0x
or0X
, 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.print(letter);
System.out.print(digit);
System.out.print(newline);
System.out.println(omega);
}
}
J9
Ω
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
.
QUIZ
Question 1:Which of the following integer literal declarations in Java represents the number 42 in binary form?
Question 2:A floating-point literal like 3.14
is treated as float
by default in Java.
Question 3:Which of the following are valid Java literal values?
Question 4:What will be printed by the following code?
char omega = '\u03A9';
System.out.println(omega);
char omega = '\u03A9';
System.out.println(omega);
Question 5:The null literal can be assigned to both primitive and reference types in Java.
Question 6:In the following code snippet, which lines produce output?
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);
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);