A Java literal is a sequence of characters (digit, letters and other characters) that represent constant value to be stored in variable.
A literal is the source code representation of a fixed value.
Integer Literals
Floating-Point Literals
Character Literals
String Literals
Boolean Literals
An integer literal is of type long if it ends with the letter L or l; otherwise it is of type int.
Values of the integral types byte, short, int, and long can be created from int literals.
There are four ways to represent integer number in the Java language :
Decimal ( base 10 )
int x = 20;
Octal ( base 8 )
int x = 076; (starts with 0)
Hexadecimal ( base 16 )
int x = 0x13A; (starts with 0x)
Binary (base 2 )
int x = 0b101; (starts with 0b)
Floating-Point numbers are defined as a number, a decimal symbol, and one or more numbers representing the fraction.
For Example:Floating-points literals are defined as double (64 bits) by default, so if you want to assign a floating-point literal to a variable of type float ( 32 bits),you must attach the suffix F or f to the number;
For Example:A character literal is represented by a single character in single quotes.
For Example:You can also type in the Unicode value of the character, using the Unicode notation of prefixing the value with \u.
For Example:In Java, string literals are enclosed within double quotes.
For Example:String literals are not only a sequence of character as in 'C', these are objects of type java.lang.String
. For Example:Boolean literals are the source code representation for boolean value. A boolean value can only be defined as true or false.
Although in 'C' (and some other languages) it is common to use number to represent true or false, this will not work in Java.
 boolean b = true ;//Legal