In Java, a Literal is a value of boolean, numeric, character, or string data. Any constant value that can be assigned to the variable is called a literal.
// Here 100 is a constant/literal.
int x = 100;
Types of Literals in Java
Java supports the following types of literals:
1. Integral Literals in Java
For Integral data types (byte, short, int, long), we can specify literals in four ways, which are listed below:
1.1 Decimal literals (Base 10): In this form, the allowed digits are 0-9.
int x = 101;
1.2 Octal literals (Base 8): In this form, the allowed digits are 0-7.
// The octal number should be prefix with 0.
int x = 0146;
1.3 Hexadecimal literals (Base 16): In this form, the allowed digits are 0-9, and characters are a-f. We can use both uppercase and lowercase characters, as we know that Java is a case-sensitive programming language, but here Java is not case-sensitive.
// The hexa-decimal number should be prefix
// with 0X or 0x.
int x = 0X123Face;
1.4. Binary literals: From 1.7 onward, we can specify literal value even in binary form also, allowed digits are 0 and 1. Literals value should be prefixed with 0b or 0B.
int x = 0b1111;
public class Geeks {
public static void main(String[] args)
{
// decimal-form literal
int a = 101;
// octal-form literal
int b = 0100;
// Hexa-decimal form literal
int c = 0xFace;
// Binary literal
int d = 0b1111;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}
}
Output
101 64 64206 15
Note: By default, every integral literal is of int type. To specify it as long, add the suffix L or l. There’s no explicit way to define byte or short literals, but if an integral value assigned is within their range, the compiler treats it automatically as a byte or short literal.
2. Floating-Point Literal in Java
For Floating-point data types, Java supports decimal as well as hexadecimal floating-point literals. Octal floating-point literals are not supported.
2.1 Decimal literals(Base 10): In this form, the allowed digits are 0-9.
double d = 123.456;
public class Geeks {
public static void main(String[] args){
// decimal-form literal (float type suffix 'f' or 'F' is required)
float a = 101.230f;
// It is a decimal literal despite the leading zero
float b = 0123.222f;
// Hexadecimal floating-point literals ARE supported in Java (since Java 5).
// They use 'p' or 'P' to specify the binary exponent.
double c = 0x1.8p3; // 1.8 (hex) × 2^3 = 12.0
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
Output
101.23 123.222 12.0
Note: By default, floating-point literals are of double type. To assign them to a float, use the suffix f or F. You may optionally use d or D for double. Hexadecimal floating-point literals are supported in Java (since Java 5) and use
porPto specify the power of 2 exponent.
3. Char Literals in Java
For char data types, we can specify literals in four ways which are listed below:
1 Single quote: We can specify literal to a char data type as a single character within the single quote.
char ch = 'a';
2. Char literal as Integral literal: we can specify char literal as integral literal, which represents the Unicode value of the character, and that integral literal can be specified either in Decimal, Octal, and Hexadecimal forms. But the allowed range is 0 to 65535.
char ch = 062; // Octal literal representing character with Unicode code 50 (which is '2')
3. Unicode Representation: We can specify char literals in Unicode representation ‘\uxxxx’. Here xxxx represents 4 hexadecimal numbers.
char ch = '\u0061';// Here /u0061 represent a.
4. Escape Sequence: Every escape character can be specified as char literals.
char ch = '\n';
public class Geeks{
public static void main(String[] args){
// single character literal within single quotes
char ch = 'a';
// invalid octal literal (causes compilation error)
// char b = 0789;
// Unicode representation
char c = '\u0061';
System.out.println(ch);
// commented out due to error
// System.out.println(b);
System.out.println(c);
// Escape character literal
System.out.println("\" is a symbol");
}
}
Output
a a " is a symbol
4. String Literals in Java
Any sequence of characters within double quotes is treated as String literals.
String s = "Hello";
String literals may not contain unescaped newline or linefeed characters. However, the Java compiler will evaluate compile-time expressions, so the following String expression results in a string with three lines of text.
String text = "This is a String literal\n"
+ "which spans not one and not two\n"
+ "but three lines of text.\n";
Illustration:
public class Geeks{
public static void main(String[] args){
String s = "Hello";
// Without double quotes, it is treated as a variable and causes a compiler error
// String s1 = Hello;
System.out.println(s);
// commented out due to error
// System.out.println(s1);
}
}
Output
Hello
5. Boolean Literals in Java
Only two values are allowed for Boolean literals, i.e., true and false.
boolean b = true;
boolean c = false;
public class Geeks{
public static void main(String[] args){
boolean b = true;
boolean c = false;
// The following lines cause compilation
// errors and are commented out
// boolean d = 0;
// boolean e = 1;
System.out.println(b);
System.out.println(c);
// System.out.println(d);
// System.out.println(e);
}
}
Output
true false
