Literal : Any constant value which can be assigned to the variable is called as literal/constant.
// Here 100 is a constant/literal. int x = 100;
For Integral data types (byte, short, int, long), we can specify literals in 4 ways:-
- Decimal literals (Base 10) : In this form the allowed digits are 0-9.
int x = 101;
- Octal literals (Base 8) : In this form the allowed digits are 0-7.
// The octal number should be prefix with 0. int x = 0146;
- Hexa-decimal 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;
- Binary literals : From 1.7 onward we can specify literals value even in binary form also, allowed digits are 0 and 1. Literals value should be prefixed with 0b or 0B.
int x = 0b1111;
101 64 64206 15
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 within the range of byte then the compiler treats it automatically as byte literals.
For Floating-point data types, we can specify literals in only decimal form and we cant specify in octal and Hexa-decimal forms.
Decimal literals(Base 10) : In this form the allowed digits are 0-9.
double d = 123.456;
101.230 123.222 Error: malformed floating point literal
NOTE: By default every floating point literal is of double type and hence we cant assign directly to float variable. 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. Of course this convention is not required.
For char data types we can specify literals in 4 ways:
- Single quote : We can specify literal to char data type as single character within single quote.
char ch = 'a';
- Char literal as Integral literal : we can specify char literal as integral literal which represents Unicode value of the character and that integral literals can be specified either in Decimal, Octal and Hexadecimal forms. But the allowed range is 0 to 65535.
char ch = 062;
- Unicode Representation : We can specify char literals in Unicode representation ‘\uxxxx’. Here xxxx represents 4 hexadecimal numbers.
char ch = '\u0061';// Here /u0061 represent a.
- Escape Sequence : Every escape character can be specify as char literals.
char ch = '\n';
a error:Integer number too large a " is a symbol
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";
Hello error: cannot find symbol symbol: variable Hello location: class Test
Only two values are allowed for Boolean literals i.e. true and false.
boolean b = true;
true false error: incompatible types: int cannot be converted to boolean error: incompatible types: int cannot be converted to boolean
NOTE : 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 operations are known as Mixed Mode operation.
Explanation : Whenever we are performing addition between a string and integer then the overall result is converted into string. In 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"
This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.