In Java, a Literal is a synthetic representation of boolean, numeric, character, or string data. It is a medium of expressing particular values in the program, such as an integer variable named "x" is assigned an integer value in the following statement.
// Here 100 is a constant/literal.
int x = 100;
Note: Any constant value that can be assigned to the variable is called a literal/constant.
Types of Literals in Java
Java supports the following types of literals:
- Integral Literals
- Floating-Point Literals
- Char Literals
- String Literals
- Boolean Literals
Integral Literals in Java
For Integral data types (byte, short, int, long), we can specify literals in four ways, which are listed below:
1. Decimal literals (Base 10): In this form, the allowed digits are 0-9.
int x = 101;
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;
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;
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;
Example:
Java
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);
}
}
Note: By default, every literal is of int type, we can specify explicitly as long type by suffixed with l or L. There is no way to specify byte and short literals explicitly but indirectly we can specify. Whenever we are assigning integral literal to the byte variable and if the value is within the range of byte then the compiler treats it automatically as byte literals.
Floating-Point Literal in Java
For Floating-point data types, we can specify literals in only decimal form, and we can not specify in octal and Hexadecimal forms.
1. Decimal literals(Base 10): In this form, the allowed digits are 0-9.
double d = 123.456;
Example:
Java
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 NOT supported in Java and will cause an error
// This line causes compilation error
// float c = 0x123.222;
System.out.println(a);
System.out.println(b);
// Commented out due to error
// System.out.println(c);
}
}
Note:
- By default, every floating-point literal is of double type, and hence we cant assign directly to the float variable without suffix f and F. But we can specify floating-point literal as float type by suffixed with f or F. We can specify explicitly floating-point literal as double type by suffixed with d or D(but suffix is optinal).
- Hexadecimal floating-point literals are not supported in Java, and attempting to use them causes a compilation error.
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')
Note: If invalid digits are used for octal (means digit other than 0=7), it will it will cause a compile-time error, for example:
char b = 0789; // Invalid octal literal due to digits 8 and 9 — causes compile-time error
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';
Example:
Java
// Java program to illustrate the
// application of char literals
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");
}
}
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.
Example:
String text = "This is a String literal\n"
+ "which spans not one and not two\n"
+ "but three lines of text.\n";
Illustration:
Java
// Java program to illustrate the
// application of String literals
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);
}
}
Boolean Literals in Java
Only two values are allowed for Boolean literals, i.e., true and false.
boolean b = true;
boolean c = false;
Example:
Java
// Java program to illustrate the
// application of boolean literals
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);
}
}
String Concatenation and Mixed Mode Operations
When we are performing concatenation operations, then the values in brackets are concatenated first. Then the values are concatenated from the left to the right. We should be careful when we are mixing character literals and integers in String concatenation operations and this type of operation are known as Mixed Mode operation.
Example:
Java
// Java program to illustrate the behaviour of
// char literals and integer literals when
// we are performing addition
public class Geeks {
public static void main(String[] args)
{
// ASCII value of 0 is 48
int first = '0';
// ASCII value of 7 is 55
int second = '7';
System.out.println("Geeks!" + first +
'2' + second);
}
}
Explanation: Whenever we are performing addition between a string and integer, the overall result is converted into a string. The above program evaluation is done in the following way:
"Geeks!" + first + '2' + second
"Geeks! " + 48 + '2' + 55
"Geeks!48" + '2' + 55
"Geeks!482" + 55
"Geeks!48255"
Similar Reads
Array Literals in Java Literal is just unlikely any constant value that can be assigned to a variable. Illustration: int x = 10; // Here 10 is a literal. Now let us stick to the array literals that are just like primitive and string literals java also allows us to use array literals. Java defines special syntax that allow
3 min read
Separators in Java Be it a programmer who is unaware of this concept but is indulging in every program. Separators help us defining the structure of a program. In Java, There are few characters used as separators. The most commonly used separator in java is a semicolon(;). Let us explore more with the help of an illus
2 min read
Wildcards in Java The question mark (?) is known as the wildcard in generic programming. It represents an unknown type. The wildcard can be used in a variety of situations such as the type of a parameter, field, or local variable; sometimes as a return type. Unlike arrays, different instantiations of a generic type a
4 min read
Shift Operator in Java Operators in Java are used to performing operations on variables and values. Examples of operators: +, -, *, /, >>, <<. Types of operators: Arithmetic Operator,Shift Operator,Relational Operator,Bitwise Operator,Logical Operator,Ternary Operator andAssignment Operator. In this article, w
4 min read
var keyword in Java The var reserved type name (not a Java keyword) was introduced in Java 10. Type inference is used in var keyword in which it detects automatically the datatype of a variable based on the surrounding context. The below examples explain where var is used and also where you can't use it. 1. We can decl
4 min read
Basic Operators in Java Java provides a rich operator environment. We can classify the basic operators in java in the following groups: Arithmetic OperatorsRelational OperatorsBitwise OperatorsAssignment OperatorsLogical Operators Let us now learn about each of these operators in detail. 1. Arithmetic Operators: Arithmetic
10 min read