What is literal in Java?
Literals are the constant values that can be assigned to a variable.
Types of literals:
int x = 060;
System.out.println(x);
When you use 0 as a prefix in Java, then Java treats this value as an octal value.
So, the output of the above code will be 48.
But when you write code like this
int x = 078;
then this will give an error (the literal of type int is out of range) because the range of octal value is 0-7.
int y = 0x45b;
System.out.println("y is "+y);
This line of code will print y is 1115.
In hexadecimal literal, Java is not case-sensitive.
So int y = 0x45b; and 0X45B; both are allowed.
Range of hexadecimal literal is 0-9 and a-f or A-F
When we use 0b as a prefix then the value becomes binary literal.
int g = 0b1010;
System.out.println("g is "+g);
The output will be 10;
The range of binary literal is 0-1.
Associate Professor - Computer Science
4 个月Keep more practice Devidas