In the previous article, I have explained assigning the out of range integer values to the byte, short and int data types.
In this article, I am going to explain the different types of literals in Java.
Java for Testers – Different types of Literals
In Java, literals are the representation of numeric, boolean, string and character data as shown below:
Let me explain the different types of Literals one after the other with proper examples below:
Numeric literals are nothing but numbers.
There are four types of Numeric Literals:
- Integer Literals
- Long Literals
- Floating Point Literals
- Double Literals
I will explain all these Numeric Literals one after the other:
Integer Literals are nothing but the numerical values in the range of – 2,147,483,648 to 2,147,483,647
byte a = 9;
In the above example, the numerical value 9 is the integer literal.
short b = 12456;
In the above example, the numerical value 12456 is the integer literal.
int c = 369456;
In the above example, the numerical value 369456 is the integer literal.
Long literals are the numerical values in the range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 which have L letter at the end of their value.
long d = 9223372036854L;
In the above example the numerical value having L letter at its end (i.e. 9223372036854L) is the Long literal.
Floating Point Literals
Floating-point literals are the decimal values which have f letter at the end of their value.
float e = 3.69f;
In the above example, the decimal value having f at its end (i.e. 3.69f) is the floating-point literal.
Double Literals are the decimal values which are double the size of floating-point literals.
double f = 3.69;
In the above example, the decimal value 3.69 is the double type literal.
Boolean literals have only two possible values i.e. either true or false.
boolean g = true;
In the above example, the boolean value true is the boolean literal.
Character literals are single letter values enclosed in single quotes.
char h = 's';
In the above example, the character value s in single quotes is the character literal.
String literals are a sequence of characters in double-quotes.
String i = "QAFox - Software Testing Tutorials";
In the above example, the string value QAFox – Software Testing Tutorials in double-quotes is the String literal.
Additional information about Literals in Java:
Integer Literals versus Long Literals
Apart from the above-explained concepts on Literals, we have to also understand the below explained additional information about Literals:
The numerical values assigned to the byte, short, int and long are by default integer literals, until you add L to the end of the literal to make it a long literal.
Examples for integer literals:
byte a = 9; short b = 99; int c = 999; long d = 9999;
In the above examples, the values 9, 99, 999, 9999 are all integer literals.
Example for long literal:
long d = 9999L;
In the above example, as we have added L at the end of the numerical value, the Integer literal has become a Long literal.
_ can be used between the numerical literals to make them readable
The usage of _ in the numerical values make them readable as shown in the below examples:
int a = 999999; int b = 9_99_999;
We can execute the below program to get the same output before and after adding _ to the numerical values as shown below:
Using Escape Characters with String Literals
Different Escape Characters like below can be used with String literals for different purposes:
\t – adds a tab in the text
\n – inserts a newline in the text
\’ – inserts a single quote in the text
\” – inserts a double quote in the text
\\ – inserts a slash in the text
Find the demonstrations for all the above Escape Characters below:
Here conclude this article on Literals in Java.
In the next article, I will explain the remaining things about data types.
- > To learn more about Java, continue to the next post (Click on Next Post link below)
- > Check complete Java Tutorial Contents here (Click here)
Please leave your questions/comments/feedback below:
Happy Learning ?
Arun Motoori (www.QAFox.com)
On a mission to help the Testing Community in all possible ways.