Literals in Java
Any constant value which can be assigned to the variable is called a Literal.
Ex:- int x = 10
int => Data type or Keyword
x => Name of Variable or Identifier
10 => Constant value or Literal
Integral Literals
For integral data types (byte, short, int, long) we can specify literal value in the following ways
1. Decimal form/Literals (base - 10)
Allowed Digits are 0 to 9
Ex:- int x = 10
2. Octal form (base - 8)
Allowed Digits are 0 to 7
Literal value should be prefixed with 0
Ex:- int x = 010
3. Hexadecimal form (base - 16)
Allowed Digits are 0 to 9, a to f
Note: for extra digits (a to f) we can use both lower case and upper case characters, this is one of very few areas where Java is NOT case sensitive.
Literal value should be prefixed with 0x or 0X (both small x and capital X is allowed).
Ex:- int x = 0X10
These are only possible ways to specify Literal value for Integral Data Types.
Question: Which of the following declarations are Valid Literals?
1. int x = 10;
2. int x = 0789;
3. int x = 0777;
4. int x = 0XFace;
5. int x = 0XBeef;
6. int x = 0XBeer;
Ans: 2 and 6 are invalid
Example:-
class Test
{
public static void main(String[] args)
{
int x = 10;
int y = 010;
int z = 0X10;
System.out.println(x+”..”y+”..”z);
}
}
Ans: 10..8..16
Programmer has choice to define value in decimal, octal and hexadecimal form but JVM will always provide the output value in decimal form.
Note:- By default every integral Literal is of int type but we can specify explicitly long type by suffixed with small l or capital L.
Example:-
int x = 10
long l = 10L
int x = 10L {CE: Possible Loss of Precision, Found: long Required: int}
long l = 10
There is no direct 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 compiler treats it automatically as Byte Literal Similarly Short Literal also.
Ex:-
byte b = 10;
byte b = 127;
byte b = 128; {CE: Possible Loss of Precision, found: int, required: byte}
short s = 32767;
short s = 32768; {CE: Possible Loss of Precision, found: int, required: short}
Ex:- int x = 10
int => Data type or Keyword
x => Name of Variable or Identifier
10 => Constant value or Literal
Integral Literals
For integral data types (byte, short, int, long) we can specify literal value in the following ways
1. Decimal form/Literals (base - 10)
Allowed Digits are 0 to 9
Ex:- int x = 10
2. Octal form (base - 8)
Allowed Digits are 0 to 7
Literal value should be prefixed with 0
Ex:- int x = 010
3. Hexadecimal form (base - 16)
Allowed Digits are 0 to 9, a to f
Note: for extra digits (a to f) we can use both lower case and upper case characters, this is one of very few areas where Java is NOT case sensitive.
Literal value should be prefixed with 0x or 0X (both small x and capital X is allowed).
Ex:- int x = 0X10
These are only possible ways to specify Literal value for Integral Data Types.
Question: Which of the following declarations are Valid Literals?
1. int x = 10;
2. int x = 0789;
3. int x = 0777;
4. int x = 0XFace;
5. int x = 0XBeef;
6. int x = 0XBeer;
Ans: 2 and 6 are invalid
Example:-
class Test
{
public static void main(String[] args)
{
int x = 10;
int y = 010;
int z = 0X10;
System.out.println(x+”..”y+”..”z);
}
}
Ans: 10..8..16
Programmer has choice to define value in decimal, octal and hexadecimal form but JVM will always provide the output value in decimal form.
Note:- By default every integral Literal is of int type but we can specify explicitly long type by suffixed with small l or capital L.
Example:-
int x = 10
long l = 10L
int x = 10L {CE: Possible Loss of Precision, Found: long Required: int}
long l = 10
There is no direct 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 compiler treats it automatically as Byte Literal Similarly Short Literal also.
Ex:-
byte b = 10;
byte b = 127;
byte b = 128; {CE: Possible Loss of Precision, found: int, required: byte}
short s = 32767;
short s = 32768; {CE: Possible Loss of Precision, found: int, required: short}
Comments
Post a Comment