⏱️ 6 min read • Beginner Level • Lesson 14
Completed on . You can revise this lesson or continue to the next topic.
A Java literal is a fixed value written directly in the source code. Literals are used to assign constant values to variables, constants, and expressions.
Before learning literals, it is useful to understand Java variables, Java data types, and Java identifiers.
In Java, a literal is the direct representation of a fixed value in a program.
For example, in the statement int age = 20;, the value
20 is an integer literal.
A literal is a fixed value written directly in Java source code.
Literals are commonly used to assign values to variables and constants.
Java supports different types of literals such as integer, floating-point, character, string, boolean, and null literals.
public class LiteralExample {
public static void main(String[] args) {
int age = 20;
double salary = 25000.50;
char grade = 'A';
String name = "John";
boolean passed = true;
System.out.println(age);
System.out.println(salary);
System.out.println(grade);
System.out.println(name);
System.out.println(passed);
}
}
20 is an integer literal.25000.50 is a floating-point literal.'A' is a character literal."John" is a string literal.true is a boolean literal.Java provides several types of literals depending on the kind of value represented.
Integer literals represent whole number values. By default, an integer literal is of type
int. If it ends with L or l, it becomes a
long literal.
Java integer literals can be written in different number systems such as decimal, binary, octal, and hexadecimal. These variants help represent numeric values in the format that best suits the problem.
public class IntegerLiteral {
public static void main(String[] args) {
int decimal = 100;
int binary = 0b1100100;
int octal = 0144;
int hex = 0x64;
System.out.println("Decimal value: " + decimal);
System.out.println("Binary value: " + binary);
System.out.println("Octal value: " + octal);
System.out.println("Hexadecimal value: " + hex);
}
}
Output:
Decimal value: 100 Binary value: 100 Octal value: 100 Hexadecimal value: 100
100, but they are written using
different number systems.
100 is a decimal literal.0b1100100 is a binary literal.0144 is an octal literal.0x64 is a hexadecimal literal.
Java allows underscores (_) inside numeric literals to improve readability.
These underscores do not affect the actual value of the number.
Numeric literal separators were introduced in Java 7. They are commonly used with large numbers, binary values, account numbers, population values, and other long numeric constants. This is also a common Java interview question.
public class NumericLiteralSeparators {
public static void main(String[] args) {
long population = 1_400_000_000L;
int amount = 10_00_000;
int binary = 0b1010_1100;
int hex = 0xCAFE_BABE;
System.out.println(population);
System.out.println(amount);
System.out.println(binary);
System.out.println(hex);
}
}
Output:
1400000000 1000000 172 -889275714
1_400_000_000L and 1400000000L represent the same value.
L, F, or D.0x or 0b.int validNumber = 10_00_000; // Valid
long validLong = 1_400_000_000L; // Valid
int validBinary = 0b1010_1100; // Valid
int invalid1 = _1000; // Invalid: starts with underscore
int invalid2 = 1000_; // Invalid: ends with underscore
double invalid3 = 12_.5; // Invalid: before decimal point
double invalid4 = 12._5; // Invalid: after decimal point
long invalid5 = 1000_L; // Invalid: before suffix
int invalid6 = 0x_CAFE; // Invalid: after prefix
Integer literals can be written in decimal, octal, hexadecimal, and binary forms.
Note: Octal literals start with0, hexadecimal literals start with
0x or 0X, and binary literals start with 0b or 0B.
Floating-point literals represent decimal values with a fractional part.
By default, floating-point literals are of type double.
To create a float literal, use suffix F or f.
public class FloatingPointLiterals {
public static void main(String[] args) {
double price = 223.56;
float discount = 12.5F;
double scientific = 1.25e3;
System.out.println(price);
System.out.println(discount);
System.out.println(scientific);
}
}
Output:
223.56 12.5 1250.0
A character literal represents a single character enclosed in single quotes. Java also supports Unicode escape sequences for characters.
public class CharacterLiterals {
public static void main(String[] args) {
char letter = 'A';
char symbol = '@';
char unicodeChar = '\u0041';
System.out.println(letter);
System.out.println(symbol);
System.out.println(unicodeChar);
}
}
Output:
A @ A
In Java, escape sequences are special character combinations that start with a backslash
(\). They are used to represent characters that are difficult to type directly,
such as a new line, tab space, backslash, single quote, or double quote.
| Escape Sequence | Meaning | Example Usage |
|---|---|---|
\n |
New Line | System.out.println("Hello\nJava"); |
\t |
Tab | System.out.println("Hello\tJava"); |
\\ |
Backslash | System.out.println("C:\\Java"); |
\' |
Single Quote | char quote = '\''; |
\" |
Double Quote | System.out.println("He said \"Hello\""); |
public class EscapeSequencesExample {
public static void main(String[] args) {
System.out.println("Hello\nJava");
System.out.println("Hello\tJava");
System.out.println("C:\\Java");
char singleQuote = '\'';
System.out.println(singleQuote);
System.out.println("He said \"Hello\"");
}
}
Output:
Hello Java Hello Java C:\Java ' He said "Hello"
\n moves text to a new line, and \t adds tab spacing.
A string literal is a sequence of characters enclosed in double quotes.
In Java, string literals create objects of the String class.
public class StringLiterals {
public static void main(String[] args) {
String message = "Java Prowess";
String greeting = "Welcome to ProwessApps";
System.out.println(message);
System.out.println(greeting);
}
}
Output:
Java Prowess Welcome to ProwessApps
Boolean literals represent truth values. Java has only two boolean literals:
true and false.
public class BooleanLiterals {
public static void main(String[] args) {
boolean isJavaEasy = true;
boolean isKeywordIdentifier = false;
System.out.println(isJavaEasy);
System.out.println(isKeywordIdentifier);
}
}
Output:
true false
0 or 1 as boolean values.
Only true and false are valid boolean literals.
The null literal represents the absence of an object reference.
It can be assigned to reference variables such as objects, arrays, and strings,
but not to primitive variables.
public class NullLiteral {
public static void main(String[] args) {
String name = null;
System.out.println(name);
}
}
Output:
null
| Literal Type | Example | Meaning |
|---|---|---|
| Integer Literal | 100, 0x13A, 0b101 |
Whole number value |
| Floating-Point Literal | 10.5, 12.5F |
Decimal number value |
| Character Literal | 'A', '@' |
Single character |
| String Literal | "Java" |
Sequence of characters |
| Boolean Literal | true, false |
Logical truth value |
| Null Literal | null |
No object reference |
double by default unless suffixed with F or f.true or false.🧠 Test your understanding with a quick quiz
Topic: Literals | Language: Java
🎉 Great job! Continue learning Java step by step.
Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.
Discussion
Ask questions, share suggestions, or discuss this lesson.