Literals:
------------- newline
Any constant value which can be assigned to the variable is called literal.
1. Integer Literals:
For integral data types ( byte, short, int, long ), we can specify a literal value in the following base...
a) Decimal literal (base 10):
Allowed digits are 0 to 9.
e.g. int x = 10;
b) Octal literal (base 8):
Allowed digits are 0 to 7.
Literal should be prefixed with '0' (Zero).
e.g. int x = 010;
c) Hexa decimal literal (base 16):
Allowed digits are 0 to 9, a to f. For extra digits (a to f), we can use both lowercase and uppercase characters. This is one of the very few areas where java is not case sensitive. The literal value should be prefixed with '0x' or '0X'.
e.g. int x = 0X10;
These are only possible ways to specify a literal value for integral data types.
Que:- Which of the following are valid/Invalid?
int x = 10; Valid
int x = 0786; Invalid
int x = 0777; Valid
int x = 0XFace Valid
int x = 0XBecf Valid
int x = 0XBeer Invalid
Que:- What will be the output of the following code?
Ans:-
10--8--16
Note:- By default every integral literal is of int type. But we can specify the explicitly long type by sufficed with 'l' or 'L'.
Que:- Which of the following are valid/Invalid?
int x = 10; Valid
long l = 10l; Valid
int x = 10L; Invalid
long l = 10; Valid
2. Floating-point Literal:
By default, every floating-point literal is of double type and hence we can't assign directly to the float variable. But we can specify floating-point literal as float type by sufficed with 'f' or 'F'.
e.g. float f = 123.456F; Valid
float f = 123.456; Invalid
Note: We can specify explicitly floating-point literals as double type by suffixed with 'd' or 'D'. Of course, this conversion is not required.
e.g. double d = 123.456d; valid
We can specify floating-point literal only in decimal form and we can't specify in octal and hexadecimal forms.
We can assign integer literal directly to floating-point variables under that integral literal can be specified either in decimal, octal or hexadecimal form.
double d = 0786; Invalid
double d = 0XFace; Valid
double d = 0786.0; Valid
double d = 0XFace.0; Invalid
double d = 10; Valid
double d= 0777; Valid
We can specify floating-point literal even in exponential form(scientific notation).
e.g.
double d = 1.2e3; Valid
float f =1.2e3; Invalid
float f = 1.2e3F Valid
3. boolean literals:
The only allowed values for boolean literals are true and false.
4. char literals:
We can specify char literal as a single character within single quotes.
e.g. char ch = 'a'; valid
char ch = 'ab'; Invalid
e.g.
char ch = 97;
System.out.println(ch);
output: a
- We can specify char literal as integer literal which represents the Unicode value of that character. Integral literal can be specified either in octal, decimal, or hexadecimal form. But the allowed range is 0 to 65535.
char ch = 0XFace; Valid
char ch = 0777; Valid
char ch = 65535; Valid
char ch = 65536; Invalid
- We can represent char literal in Unicode representation which is nothing but '\uxxxx'. Here xxxx is the 4 digit hexa decimal number.
char ch = '\u0061';
System.out.println(ch);
Output: a
- Every escape character is a valid char literal. Escape characters are as follows:
\t ------------- horizontal tab
\r ------------- Carriage return
\b ------------- Backspace
\f ------------- form feed
\' ------------- Single quotes
\" ------------- Double quotes
\\ ------------- Backslash
Que:- Which of the following is valid/Invalid?
char ch = 65536; Invalid
char ch = 0XBeer; Invalid
char ch = \uface; Invalid
char ch = '\ubeef'; Valid
char ch = '\m'; Invalid
char ch = '\n'; Valid
char ch = '\iface'; Invalid
5. String literals:
Any sequence of characters within double quotes is treated as a string literal.
e.g. String s = "Omkar";
- Enhancement with respect to literals in 1.7 version:
For integral data types, until 1.6 version, we can specify a literal value in decimal, octal, and hexadecimal form. But from 1.7 version onward, we can specify literal value even in binary form also. Allowed digits are 0 and 1. The literal value should be prefixed with '0b' or '0B'.
e.g.
int x = 0B1111;
System.out.println(x);
Output: 15
2) Usage of underscore in numeric literals:
From 1.7 version onward, we can use the underscore (_) symbol between digits of numeric literal.
e.g.
double d = 1_23_456.7_8_9;
double d = 1_234_56.7_8_9;
The main advantage of this approach is the readability of code will be improved. At the time of compilation, these underscore symbols will be removed automatically. Hence after compilation, the above lines will become the same.
We can use more than one underscore symbol also between the digits.
e.g. double d = 1___23__456.7_8_9;
We can use '_' only between digits, if we are using anywhere else we will get a compile-time error. The following statements will generate the compile-time error.
double d = _1_2.3;
double d = 12_2_.3;
double d = 123.4_;
Note: The 8-byte long value we can assign to a 4-byte float variable because both are following different memory representations internally.
e.g. float f = 10L;
System.out.println(f);
Output: 10.0
Comments
Post a Comment