These is a seven-part series intended to cover all concepts regarding the fundamentals of core Java. Java is considered among the oldest set of languages and is still in demand today, i believe newer and effective ways should be created to ensure the learning process evolves with the parlance of our ever evolving tech-age. Codenewbie community affords users this opportunity and i am grateful for it. I will release a new part of the series on a weekly basis, please enjoy😊, leave suggestions🤲 and questions are appreciated(they make me a better developer and person).
//[SERIES BEGIN HERE.]
In this section, we will discuss Literals. Once you understand this section, you won’t need to go back to study Data types as the principles guiding Data types will be clearly revealed here.
From the above diagram(a java statement),
• “int” – A Data type/keyword.
• “x” – Variable name/identifier.
• “14” – Literal/Constant.
In Java, data types clearly define the value a variable can take.
For example; if a variable has “int” data type, subsequently, it will only take integer or integer-esque values.
Now, let’s dive deep into literals.
What is a literal?
Any constant value which can be assigned to a variable is called a “literal”.
The following are types of literals.
o Integral literals
o Floating point literals
o Boolean literals
o Char literals
o String literals
Lastly, we will learn about the two enhancements added to the JDK version 1.7(Java development toolkit) with respect to literals.
Before we begin explaining each of the literals, I want us to keep this rule of literal values in mind which is, we can assign lower data type values to higher data type variables.
This will be duly explained through the course of this section, observe the statement below;
As you can see above, a Character – “char” literal value is assigned to the Integral data type “int”, this will not give a compile time error simply because the int data type (4 byte) is of a higher data type than the char (2 byte) in the general assignment of literal values.
Now with the understanding we have of literals, let’s briefly explain the different literal types.
o Integral Literals
For integral data types (byte, short, int and long), we can specify literal values in the following forms;
i. Decimal form (base – 10);
The allowed digits are 0-9.
iii. Hexa-decimal form(base – 16);
The allowed digits are 0-9, and a-f (lower and upper case). This is one of the few aspects of java where case sensitivity is lenient. The literal values should be prefixed with “0x” or “0X”.
Until the enhancements, these were the only possible ways to specify literal value for integral data types.
o Floating point literals
By default, every floating-point literal is of a double type and hence, we can’t assign directly to the float variable. In every java statement that contains a float range data type, we must suffix the float type with “f” or “F”.
The following rules guide the assignment of floating-point literals:
i. We can explicitly specify floating-point literals as double data type by suffixing with small “d” or “D”. Of course, this convention is not compulsory, but for the sake of good programming practice.
ii. We can specify floating point literals only in decimal form (the octal and hexa-decimal forms are excluded).
iii. We can assign integral literals directly to floating point variables, and said integral literals can be specified either in decimal, octal and hexa-decimal forms.
iv. We can’t assign floating point literals to integral types.
v. We can specify floating point literals in exponential form.
Because they are assignable to higher data types, char literals can be specified as integral literals which represents Unicode values of various characters within the range “0 – 65535”. The specified integral literals can also either be in decimal, octal or hexa-decimal forms.
these are the four ways to specify a char literal.
i. ‘A’ Single character within single quote.
ii. 67 Corresponding unicode value is printed.
iii. ‘\u0061\’ Unicode representation.
iv. \n Escape character.
For a better understanding, try this exercise. Which of the following are valid?
In conclusion, we would talk about the two enhancements introduced in the version 1.7 - with respect to literals.
• Binary form expression
For integral data types, far up till the version 1.6, we could only specify literal value in decimal, octal and hexa-decimal forms, but from the 1.7 version upwards, another form was introduced called the “binary” form.
The allowed digits in this form are “0” and “1”, and also the literal value must be prefixed with “0b” or “0B”.
• Usage of “_” symbol
The underscore symbol usage in numeric form was also introduced in the version 1.7. The only rule is, we can only use it between digits of numeric literals, and the advantage of this enhancement is to improved the readability of the code.
Note: In the time of compilation, the underscore symbols added are removed automatically and the compiler displays the resulting output without them.