Literals in Java
Literals in Java
int a = 5;
- In the above expression, a is a variable but 5 is literal.
- Literal can be of many types, we will discuss all types of literal one by one.
Integral Literal
Any integer constant is treated as integral literal. it can be written in 3 formats.
- int a = 10; (decimal format)
- int a = 010; (octal format, equivalent to 8)
- int a = 0x10; (hexadecimal format, equivalent to 16)
Note:
- Octal constants can contain digits from 0 to 9.
- Hexadecimal constants can contains digits from 0 to 9 and a to f, where a to f can be uppercase or lowercase.
Example - 1:
int a = 10; (✓)
int a = 0678; (✕) // Compile time error, number too large
int a = 0777; (✓)
int a = 0xace; (✓)
int a = 0xBeer; (✕) // Compile time error, number too large
Example - 2:
int a;Important Notes
a = 10 + 010 + 0x10;
System.out.println(a); // 34, because 010 = 8 and 0x10 = 16 so 10 + 8 + 16
- We(programmer) have option to write integer constant in decimal, octal and hexadecimal format but JVM always return integer type value in decimal format.
- By default all integer literals are integer type, to specify long type literal it must be suffixed with 'l' or 'L'.
Example - 3:
int a = 10; (✓)
long a = 10L; (✓)
int a = 10L; (✕) // Compile time error - Incompatible types, required int found long.
Type Conversion
- In Java Narrowing is not allowed, only widening is allowed implicitly (Automatically).
- If anywhere you perform Narrowing a compile time error occurred, possible loss of precision.
- If you want to perform Narrowing the you can do it explicitly.
Example-1
int a = 10L;
System.out.println(a); // Compile time error, possible loss of precision.
Example - 2
byte b = 10;
System.out.println(a);
Example - 3
byte b = 128;
System.out.println(b); // Compile time error, possible loss of precision.
Example - 4
byte b;
b = 5 + 6; // Compile time error, possible loss of precision
System.out.println(b);
Example - 5
byte b;
b = (bye)(5 + 6); (✓)System.out.println(b);
Some important Points
- In java, if a integer constant is assigned to lower data type than int (byte / short), then if constant is within the range of lower data type, it is allowed otherwise compile time error occurred.
- If arithmetic is performed with any integer type (byte / short / int) result to integer type value, and if assigned to byte and short type directly it would be a compile time error.
Floating point literals
- 54.5, 0.009, and -0.9877 all are floating points constants.
- floating point literals can be written in two ways
- Fixed format. (Ex: 0.007)
- Exponential format. (Ex: 0.7 E 4)
0.7 E 4 is equivalent to 0.7 * 104.
Example - 1
float f = 10.087; (✕) // Compile time error - Incompatible types, required float found double.Note:
float f = 10.087f; (✓)
double d = 10.087; (✓)
double d = 10.087d; (✓)
- Every floating point constant is by default of double type.
- To treat floating point constant of type float suffix with f or F.
Example - 2
double d = 123.456; (✓)
double d = 0123.456; (✓), it is allowed due to it is treated as decimal value.
double d = 0x123.456; (✕), malformed floating value, decimal value can't be written in hexadecimal format.
Example - 3
double d = 0678; (✕) malformed integer value, because octal can't contain 8.
double d = 0x678; (✓) Well formed integer value assigned in double so it is allowed.
Example - 4
double d = 1.1e4; (✓)
float f = 1.1e4; (✕) because of narrowing
float f = 1.1f4f; (✓)
boolean literal
Example - 1:
boolean b = true; (✓)
boolean b = 1; (✕) compile time error, incompatible type required boolean found int.
boolean b = True; (✕) compile time error, can't find symbol True.
boolean b = "true"; (✕) compile time error, incompatible type required boolean found String.
Comments