A literal is nothing but a value which is stored into a variable in the program.
int x =1520;
char c =”x”;
boolen b =false;
In the above statement the right hand side values are called literals. java support different types of literals such as:
Integer literal represent the fixed integer value such as 10,15,-35,1234 etc.
All these numbers belongs to decimal system which uses 10 digits (from 0 to 9) to represent any number.
Suppose we want to write an integer in octal number system (octal number system uses 8 digit from 0 to 7) then we should prefix 0 before the number. To represent hexadecimal number (hexadecimal number system uses 16 digit from 0 to 9 and from A to F ) we should prefix 0 before the value.
int x =25; // The number 25 in decimal
int y =031; // The number 25 in octal
int z =0x19; // The number 25 is in hexadecimal
Floating literal represent the decimal value with a fractional component.
Like 5.2,0.0004,2.15 etc .while writing the floating literals we can use E or e for scientific notation, F or f for float literal and D or d for double literal
double x =345.67;
Character literals represents the following
General characters like q, b, t.7.9, ets.
Special characters like ?,@, etc.
Unicode characters like \, U, 0042 (this represent ‘a’ in ISO Latin-1 character set).
Character literals should be enclosed in single quotation mark.
String literals represent objects of string class. For example vivtech, lakshya, rajeshKumar, BQ1210, etc comes under string literals which can be directly stored into a string object.
Boolean literals represent only two values such as True and False that means we can store either true or false into a boolean type variables.