Literals
Literals
What is a Literal?
Literal is a constant.
Ex: 10, 20.5, ‘a’, “zen”, true, etc.,
In variable assignment,
as an expression operand,
as a method argument,
as a return value.
Types of Literals:
Java supports 7 types of literals. They are
For a variable, we must specify the type by using a data type explicitly.
But for every literal, an inbuilt data type is assigned by Java. That data type is known to
the compiler and JVM.
For example:
10 is int type
10.5 is double type
‘a’ is char type
Working with Integer Literals
All integer literals are by default considered int type.
For example:
10, 20, 30, etc., are considered by the compiler and JVM as int type.
If we want to treat them as byte or short or long type, we must use either assignment
operator(=) or cast operator or suffix letter.
When we use binary, octal, hexa-decimal literals in program, they are converted to
decimal and they are stored in variable, used in validations, calculations and printed
Internally compiler converts Binary, Octal, Hexa-Decimal literals to decimal by using
their base value as shown below.
Note on Hexa-decimal number system:
Since 16 symbols are used, 0 to F, the notation is called Hexa-Decimal. The first 10
symbols are the same as in the decimal system, 0 to 9 and the remaining 6 letters are
taken from the first 6 letters of the alphabet sequence, A to F, where A represents 10, B is
11, C is 12, D is 13, E is 14 and F is 15.
The numbers from all the number systems are by default are of int type. We can store
them in an int type variable. We can also store them in byte/short variable provided
they are in the range of byte/short variable. We can assign int binary, octal, hexa-
decimal to the int variable.
We can represent decimal number as int, long, float and double.
We can represent binary number as only int and long type. If we try to represent it
with float or double by suffixing F, D or .0, then the compiler will throw an error.
Similar to decimal, We can also represent hexa-decimal numbers as int and long type
only.
If we try to represent hexa-decimal as float or double by suffixing it with F, D,
or .0, we get compilation error.
But we don’t get CE if we suffix F or D. Here, F or D will not represent the
number as float or double. F and D are in the range of hexa-decimal, they are
replaced with 15 and 13 respectively.
o But suffix character ‘L’, we can't use 8,9 in an Octal Number. If we use compiler
throws an error because it is consider as octal Number.
We cannot use L to a floating-point Number. The Compiler will throw an error, but we
can convert floating-point number to long by using cast operator.
Binary, octal and hexa-decimal floating-point literals are not directly supported by Java.
In Java, they are typically used for integer values, not floating-point values. But we can
convert binary, octal and hexa-decimal numbers to floating-point numbers by using
cast operator.
We can assign and store binary, octal, decimal and hexa-decimal numbers in all
primitive data type variables except boolean data type.
The default datatype of these literals is int.
Here, the rule is the literal assigned to a variable must be in the range of variable. If
not, we get CE.
Primitive Data Type Conversion:
The process of changing one type of value to another type of value is called Type
Conversion.
We develop type conversion by assigning a value of one variable to a variable of another data
type.
For example: