A Java literal is a sequence of characters that represents a fixed constant value directly in source code.
A literal is the source code representation of a fixed value.
Literals are directly used to assign values to variables or constants.
An integer literal is of type long if it ends with L or l, otherwise it is of type int.
Can be written in decimal, octal, hexadecimal, or binary form:
int dec = 20; // Decimal
int oct = 076; // Octal
int hex = 0x13A; // Hexadecimal
int bin = 0b101; // Binary
Floating-point literals represent decimal values with a fractional part.
By default, they are of type double.
double d = 223.56;
float f = 223.56F; // must use F for float
A character literal is a single character enclosed in single quotes, or a Unicode escape sequence.
char a = 'a';
char c = '@';
char ch = '\u0041'; // Unicode for 'A'
String literals are enclosed in double quotes and represent objects of type java.lang.String.
String s = "Java Prowess";
Boolean literals have only two possible values: true or false.
boolean flag = true; // Legal
boolean test = false;
boolean error = 1; // Compiler Error
Topic: Literals | Language: Java