What are Literals in Java?
A Java literal is a source code representation of a fixed value. It is a constant value. The Java compiler recognizes literals and assigns them a specific data type. You type literals directly into your program.
Literals are fundamental to writing any Java program. They help you:
- Initialize variables: You assign a literal value to a variable when you declare it.
- Perform calculations: You use numeric literals in arithmetic operations.
- Define conditions: Boolean literals (true, false) are used in conditional statements.
- Represent text: String literals define text messages.
Learn Java with this complete course. It covers everything from basics like variables and control structures to advanced topics like classes, inheritance, and polymorphism.
Types of Literals in Java
Java supports several types of literals. Each type corresponds to a specific data type.
1. Integer Literals
Integer literals represent whole numbers. They can be in decimal, octal, hexadecimal, or binary format.
- Decimal Literals (Base 10): These are numbers you use every day. They contain digits 0-9.
int decimalValue = 100;
- Octal Literals (Base 8): These start with a leading zero (
0
). They contain digits 0-7.int octalValue = 0144; // Equivalent to decimal 100
- Hexadecimal Literals (Base 16): These start with
0x
or0X
. They contain digits 0-9 and letters A-F (or a-f).int hexValue = 0x64; // Equivalent to decimal 100
- Binary Literals (Base 2): These start with
0b
or0B
. They contain digits 0 or 1.int binaryValue = 0b1100100; // Equivalent to decimal 100
You can also use an underscore (_
) in numeric literals to improve readability. The underscore does not affect the value.
long bigNumber = 1_000_000_000L; // Represents one billion
By default, integer literals are of type int
. If the value exceeds the int
range, you must append L
or l
to make it a long
literal.
2. Floating-Point Literals
Floating-point literals represent numbers with a fractional part. They include a decimal point or an exponent.
- By default, floating-point literals are of type
double
. - To specify a
float
literal, appendf
orF
. - You can optionally append
d
orD
fordouble
literals, but it is not necessary.
Java
double price = 99.99; // double literal
float temperature = 25.5F; // float literal
double scientific = 1.23e-4; // 1.23 * 10^-4, also a double literal
3. Character Literals
Character literals represent a single character. You enclose them in single quotes (‘ ‘).
They can be a single character, an escape sequence, or a Unicode escape sequence.
char grade = 'A';
char newline = '\n'; // Newline escape sequence
char unicodeChar = '\u0041'; // Unicode for 'A'
Common escape sequences include:
\n
(newline)\t
(tab)\b
(backspace)\r
(carriage return)\'
(single quote)\"
(double quote)\\
(backslash)
4. String Literals
String literals represent a sequence of characters. You enclose them in double quotes (” “).
String greeting = "Hello, Java!";
String emptyString = "";
String path = "C:\\Program Files\\Java"; // Use \\ for backslash
Java treats string literals as String
objects. The Java compiler creates a String
object for each string literal.
5. Boolean Literals
Boolean literals represent logical values. Java has only two boolean literals:
true
false
boolean isJavaFun = true;
boolean hasError = false;
6. Null Literal
The null
literal represents the absence of an object. You can assign null
to any reference type.
null
is not a data type. It is a special literal.
String name = null;
Object myObject = null;
You cannot assign null
to primitive types.
Conclusion
Literals are constant values directly embedded in your Java code. They allow you to define numbers, characters, text, and boolean states. Understanding different literal types helps you write correct and readable Java programs. You use them every day to assign values and perform operations.