JAVA Literals ( Detailed Explanation)

In this tutorial, we will dive deeper into the details of Java Literals and will provide you a complete overview of its types. Literals are data items that never change their value during the program run. Literals are also known as constant.

JAVA Literals ( Detailed Explanation)

In this tutorial , we will learn more about JAVA literals . As we have already had a sneak peek about JAVA literals in last tutorials so now firstly let us recap what we have studied about JAVA literals in the last tutorials and then we will study more about different kinds of literals in greater  detail.

Literals are data items also called Constants . Their value does not change during the time of program execution.

JAVA allows several type of Literals:

  1. Integer Literals
  2. Character Literals
  3. Floating-point Literals
  4. String Literals
  5. Boolean Literals
  6. Defining Literals

Let us now discuss about each type of literal in a greater detail.

Integer Literals

Integer Literals are whole numbers without any decimal or fractional part. The method writing integer literals is that it must have at least one digit and it should not contain any decimal point. It can be positive ( + ) or negative ( - ). The default type is positive which means number without any sign is considered as positive. They cannot have commas in it.

Java allows four types of Integer Literals:

  1. Decimal Literals ( Base 10) : Any number consisting the combination of digits belonging to sequence 0 to 9. Example: 123, +4 , -5 , +23 etc.
  2. Octal Literals ( Base 8) : Any number consisting the combination of digits belonging to sequence 0 to 7. Note : An octal number should always be preceded with 0. Example: 8 is written as 010 as an octal integer.
  3. Hexa-decimal Literals ( Base 16 ) : In these types of literals any number consisting the combination of digits belonging to range 0 to 9 and characters in the range a to f ( or A to F  as in this we can ignore case). Note : A hexa-decimal number should always be preceded with 0X  or 0x. Example: decimal, 12 will be written as 0XC as a hexadecimal integer etc.
  4. Binary Literals : It is recently added from Java 1.7 . Integer Literals can also be expressed as binary numbers( 0 and 1). Note: A binary literal should always be preceded with 0B or 0b. Example : 0b000101 etc.

Character Literals

There are 4 ways of specifying Character Literals in JAVA , Lets study about them in greater detail:

  1. Single Quote : Mostly we specify a character literal as a single character written withing single quotes.  
    char abc = 'c';
  2. Character as Integer : We can specify the character literals as integers by replacing them with their Unicode values and can specified as decimal , octal or hexa-decimal integer literal that is from 0 to 65535.
    char chr = 023;
  3. Unicode Representation : We can specify character in their Unicode representation.
    char abc = '\u0061'; // Here u0061 represents 'a'​
  4. Escape Sequences ( Non-Graphic characters):  They are represented by using backslash(\) followed by any number of characters.
    Escape Sequence Non Graphic Meaning
    \a Audible Bell(alert)
    \b Backspace
    \f Form Feed
    \n New Line or Liinefeed
    \r Carriage Return
    \t Horizontal Tab
    \v Vertical Tab
    \\ Backslash
    \' Single Quote
    \" Double Quote
    \? Question marks
    \On Octal Number
    \xHn Hexa-decimal Number
    \o null

Floating-point Literal

Floating literals are also called real constants. Real constants are numbers having fractional parts. They can be written in one of the two forms called fractional form or the exponential form.

Example 2.0, 17.5, -13.0, -0.00873, 125E05.

double db = 123.456;

String Literals 

Multiple characters constant can be treated in two ways in JAVA. If enclosed in single quotes these are treated as character constants and if enclosed in double-quotes then they are treated as string literals.

String str = "Digital Deals Solution";

Lets discuss more about Punctuators  also.


The following characters are used as punctuators in JAVA :

[ ] { } ( ) , : ; * # =

Uses of some of them are listed below:

Brackets [ ] : Opening and closing brackets indicate single and multidimensional array subscripts.

Parentheses { } : This indiciate function call and function parameter.

Braces ( ) :  It  indicates block of code.

Now, this the end of this tutorial we will look at our first program in JAVA in the next tutorial. Till the Best of luck for your journey to learn JAVA.