HomeJava

Java for Testers – Different types of Literals

Java for Testers – Different types of Literals

<<Previous Post                    << Complete Tutorial>>                        Next Post>>

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:

Different types of literals

Let me explain the different types of Literals one after the other with proper examples below:

Numeric Literals

Numeric literals are nothing but numbers.

There are four types of Numeric Literals:

  1. Integer Literals
  2. Long Literals
  3. Floating Point Literals
  4. Double Literals

I will explain all these Numeric Literals one after the other:

Integer Literals

Integer Literals are nothing but the numerical values in the range of – 2,147,483,648 to 2,147,483,647

Example#1:

In the above example, the numerical value 9 is the integer literal.

Example#2:

In the above example, the numerical value 12456 is the integer literal.

Example#3:

In the above example, the numerical value 369456 is the integer literal.

Long Literals

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.

Example:

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.

Example:

In the above example, the decimal value having f at its end (i.e. 3.69f) is the floating-point literal.

Double Literals

Double Literals are the decimal values which are double the size of floating-point literals.

Example:

In the above example, the decimal value 3.69 is the double type literal.

Boolean Literals

Boolean literals have only two possible values i.e. either true or false.

Example:

In the above example, the boolean value true is the boolean literal.

Character Literals

Character literals are single letter values enclosed in single quotes.

Example:

In the above example, the character value s in single quotes is the character literal.

String Literals

String literals are a sequence of characters in double-quotes.

Example:

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:

In the above examples, the values 9, 99, 999, 9999 are all integer literals.

Example for long literal:

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:

We can execute the below program to get the same output before and after adding _ to the numerical values as shown below:

Different types of literals - Underscore

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:

Different types of literals - Escape1

Different types of literals - Escape2

Different types of literals - Escape3

Different types of literals - Escape4

Different types of literals - Escape5

Here conclude this article on Literals in Java.

In the next article, I will explain the remaining things about data types.

Next Steps:

  • > 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.

<<Previous Post                    << Complete Tutorial>>                        Next Post>>

If you like this article, share it on LinkedIn using the below button:

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *