Index
Java Literals
Literals are data used for representing fixed values.
Boolean Literals
In Java, boolean literals are used to initialise boolean data types. They can store two values: true and false.
Here, false
and true
are two boolean literals.
Integer Literals
An integer literal is a numeric value (associated with numbers) without any fractional or exponential part.
There are 4 types of integer literals in Java:
- binary (base 2)
- decimal (base 10)
- octal (base 8)
- hexadecimal (base 16)
Tip
Integer literals are used to initialize variables of integer types like
byte
,short
,int
, andlong
.
Floating-point Literals
A floating-point literal is a numeric literal that has either a fractional form or an exponential form.
Tip
The floating-point literals are used to initialize
float
anddouble
type variables.
Character Literals
Character literals are Unicode character enclosed inside single quotes.
Here, a
is the character literal.
Tip
We can also use Escaping Sequences as character literals. For example, \b (backspace), \t (tab), \n (new line), etc.
String literals
A string literal is a sequence of characters enclosed inside double-quotes.
Here, Java Programming
and Ciao Mamma
are two string literals.