Literals in Java – Integral, Floating-Point, Char, String, Boolean

Free Java courses with 37 real-time projects - Learn Java

Literals are an integral part of a Java programmer’s day to day life. This is a concept that every programmer must know before beginning their journey into the world of Java programming. In this article, we will take a look at literals and their types and also a few rules that we need to follow while writing, declaring a literal.

What are Literals in Java?

Literals have a constant value and a fixed data type and are often known as constants in Java. Literals are assigned to a variable to provide a value to the variable.

An example of assigning a literal to a variable is:

int age = 21;

In the above code snippet, int is the datatype, age is the variable name and 21 is the literal. So, we can say that the literal is providing the variable with a value of 21.

what are literals

Types of Literals in Java

Generally, there are 5 types of literals that can be further expanded into various other literals. We will take a look at them vividly in this section.

The five main Literal types are:

  • Integer Literal
  • Float/Double Literal
  • Character Literal
  • Boolean Literal
  • String Literal

Integer Literal:

Integer literals are basically a number sequence that doesn’t contain any decimal point in between them.

There are four types of Integer Literals:

1. Decimal Integer:

They are integers having a base value of 10; i.e; containing values between 0 to 9. It can be a positive value(+) or a negative value(-) but it cannot contain any point in between them. Example: 1000, 1234, +78, -82, etc.

int decimal_int=1234;
2. Octal Integer:

They are integers having a base value of 8; i.e; containing values between 0 to 7. All octal numbers must start with a 0. Example: 012, 077,075, etc.

int octal_int=077;
3. Hexadecimal Integer:

They are integers having a base value of 16; i.e; containing values between 0 to 15. It is a special type of integer that contains both digits as well as characters. The digits range from 0 to 9 and the numbers 10 to 15 are replaced by characters a to f. Any integer starting with 0x or 0X is considered to be a hexadecimal integer. Example: 0xff, 0x2a, 0xf1f2, etc.

int hexadec_int=0x1ff2;
4. Binary Integer:

They are integers with a base value of 2; i.e; contains only two digits 0 and 1. Binary integers start with a 0b indicating that it is a binary digit. Example: 0b100101, 0b1010101, etc.

int binary_int=0b1010101;

Note: Binary literals can be created only on Java SE 7 and above.

Program Showing all the integer literals:

package com.DataFlair.Literals;
public class Literals_Integers
{
    public void main()
    {
        int decimal_int=1234;
        int octal_int=077;
        int hexadec_int=0x1ff2;
        int binary_int=0b1010101;
        System.out.println("This is a Decimal Literal: "+decimal_int);
        System.out.println("This is an Octal Literal: "+octal_int);
        System.out.println("This is a Hexa Decimal Literal: "+hexadec_int);
        System.out.println("This is a Binary Literal: "+binary_int);
    }
}

The output of the above code:

This is a Decimal Literal: 1234
This is an Octal Literal: 63
This is a Hexa Decimal Literal: 8178
This is a Binary Literal: 85

Floating Point Literal:

Floating-point literals are values that contain a decimal point in between them. Floating-point literals are generally double data type by default. We can assign them to float data types by adding an f at the end of the value.

Example of declaring a float literal:

float val_float=1.7732f;

Example of declaring a double literal:

float val_double=1.7732; //By default the compiler assigns double datatype.
float val_double=1.7732d;
double val_double=1.7732;

Floating-point Literals can also have an exponent part. This exponential part can be declared as follow:
123E-45f

Few points to remember while declaring floating-point literals are:
1. If no suffix is present, the default data type is double.
2. F or f suffix represents a floating data type.
3. D or d suffix represents a double data type.

Few legal and illegal floating literals:

  • 123.45//Legal
  • 122.32E5//Legal
  • 231.12F//Legal
  • 1/4 // Illegal Symbol Used “/”
  • 1.7.5 //Illegal, as two decimal points used.
  • 1,234.56// Illegal, as commas are not allowed
  • 123.E4//Illegal, as E cannot precede the point.
  • 321E//Illegal, as the exponent part is incomplete.

Also, floating-point literals cannot have commas in between the digits.

Program Showing all the floating literals:

package com.DataFlair.Literals;
public class Literals_Float
{
    public void main()
    {
        float val_float=1.7732f;
        double val_double=1.7732d;
        float val_exponent=123E4f;
        System.out.println("This is a Floating Point Literal"+val_float);
        System.out.println("This is a Decimal Literal"+val_double);
        System.out.println("This is an Exponential Literal"+val_exponent);
    }
}

The output of the above code:

This is a Floating Point Literal1.7732
This is a Decimal Literal1.7732
This is an Exponential Literal1230000.0

Boolean Literal:

A boolean literal is a literal that contains only two values true and false. It is declared using the keyword boolean. It is a very useful literal to declare flag variables in different programs to terminate a looping sequence.

Program showing the boolean Literals:

package com.DataFlair.Literals;
public class Literals_boolean
{
    public void main()
    {
        boolean flag1=true;
        boolean flag2=false;
        System.out.println("This is a boolean true flag variable"+flag1);
        System.out.println("This is a boolean false flag variable"+flag2);
    }
}

The output of the above code:

This is a boolean true flag variabletrue
This is a boolean false flag variablefalse

String Literal:

A string is basically an array of characters. In java, we have a special class for strings that allows users to implement strings to a program very easily. Anything written inside a double quote is a string “”.

Example of String Literal:

String Company = “DataFlair”;//Valid String Literal
String Company = DataFlair;//Invalid as there is no double quote.

Null Literal

Strings in java can also be declared void with a special literal known as null literal. It is basically equivalent to an integer value of 0.

Program showing implementation of String Literal:

package com.DataFlair.Literals;
public class Literals_string
{
    public void main()
    {
    String company="DataFlair";
    String null_Literal=null;
    System.out.println("This is a String Literal: "+company);
    System.out.println("This is a null Literal: "+null_Literal);
   }
}

The output of the above code:

This is a String Literal: DataFlair
This is a null Literal: null

Character Literal:

Character Literals in java are represented with a single quote. The general difference between string literal and character literal is that character literal contains only one character whereas string literal contains a set of characters.

A character literal can be represented in four ways:
1.Single quote character:
Example:

char ch = ‘A’;

2. Character Literal as an Integer Literal:
Example:

char number = 0065;

3. Unicode representation of character Literal:
Example:

char uni = ‘\u0065’;

4.Escape Sequence:
It is a special kind of character literal which is preceded by a \. Each and every escape sequence has a special feature. Some of the most used escape sequences are:

Escape Sequence Functionality
\nUsed to insert a new line.
\tUsed to insert a horizontal tab.
\bUsed to insert a blank space.
\vUsed to insert a Vertical tab.
\aUsed to add a small beep sound.
\’Used to add a single quote inside a string.
\”Used to add double quotes inside a String.
\\Used to add a backslash
\rUsed for carriage return.
\?Used to add a Question mark
\0nUsed to represent an octal number
\xHnUsed to represent Hexadecimal number
\uHnUsed to represent Unicode CharacterThrough its hex code.
\0Null Character
\fUsed to represent formfeed
Few Valid Character Literals:
char ch = ‘A’;
char ch =’\u0065’
char ch= 0065
char ch=’\\’
char ch=’%’

Program implementing Character Literal:

package com.DataFlair.Literals;
public class Literal_Char
{
    public void main()
    {
        char ch='A';
        char number= 0065;
        char uni='\u0065';
        System.out.print("\nThis is a character Literal:\b"+ch);
        System.out.print("\nThis is a character Literal as Integer:\b"+number);
        System.out.print("\nThis is a character Literal as Unicode:\b"+uni);
    }
}

The output of the above code:

This is a character Literal:A
This is a character Literal as Integer:5
This is a character Literal as Unicode:e

Class literals:

There’s also a special kind of literal called a class literal, which is formed by taking a type name and appending “.class” to it; for example, String.class. This refers to the object (of type Class) that represents the type itself.
Example:

Class<String> c = String.class; 

Rules to Use underscore in Literals:

  • In the beginning, end or in between the numbers.
  • Adjacent to a decimal point in a floating-point literal.
  • Prior to an F or L suffix.
  • At positions where a string of digits is expected

Some invalid use of Underscore:

  • 1_.234
  • 1._234
  • 65_
  • 12_34_56_78_90_L
  • 0_x77
  • 0x_77
  • 0x77_

Why Use Literals?

Literal is very useful for implementation in the code as it reduces declaring constant and adding labels by doing it on the same line.

How to use Literals?

As we saw earlier as well in the discussed topics, a literal is declared along with a data type, a variable name. A literal has an = sign before it which assigns the value of the literal to the variable.

Conclusion

Literals are the basic foundation of programming and grasping its concept is essential for implementation. Without literals creating a program is next to impossible. In this article, we talked about different literals and their uses and how to implement them in our code.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

follow dataflair on YouTube

2 Responses

  1. chetan says:

    Hello! Data Flair

    floatingPoint litrals in this topic

    1/4 // Illegal Symbol Used “/” (“This is invalid “)

    we can use “/” symbol ,it will not throw any error

    please update this soon as possiable

Leave a Reply

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