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

Literals are number, text, or anything that represent a value. In other words, Literals in Java are the constant values assigned to the variable. It is also called a constant.

For example,

int x = 100;

So, 100 is literal.

There are 5 types of Literals can be seen in Java. But before we start the discussion, you should revise the concept of Variables in Java.

Don't become Obsolete & get a Pink Slip
Follow DataFlair on Google News & Stay ahead of the game

Types of Literals in Java

Literals in Java with examples

1. Integral Literals in Java

We can specify the integer literals in 4 different ways –

  • Decimal (Base 10)

Digits from 0-9 are allowed in this form.

Int x = 101;
  • Octal (Base 8)

Digits from 0 – 7 are allowed. It should always have a prefix 0.

int x = 0146;

Don’t forget to check Datatypes in Java with examples

Integral Literals in Java with example

  • Hexa-Decimal (Base 16)

Digits 0-9 are allowed and also characters from a-f are allowed in this form. Furthermore, both uppercase and lowercase characters can be used, Java provides an exception here.

int x = 0X123Face;
  • Binary

A literal in this type should have a prefix 0b and 0B, from 1.7 one can also specify in binary literals, i.e. 0 and 1.

int x = 0b1111;


package com.dataflair.literals;

public class IntegralLiteral {
  public static void main(String[] args)
    int decimalValue = 123; // decimal-form literal
    int octalValue = 01200; // octal-form literal
    int hexaDecimalValue = 0xAce; // Hexa-decimal form literal
    int binaryValue = 0b00101; // Binary literal
    System.out.println("Decimal form literal is "+decimalValue);
    System.out.println("Octal form literal is "+b);
    System.out.println("Hexa-decimal form literal is "+hexaDecimalValue);
    System.out.println("Binary literal is "+binaryValue);



We can specify explicitly as long type by suffixed with l or L but there is no way to specify byte and short, but if it’s in the range the compiler automatically treats it as a byte.

Do you know what is Java Abstract Data Types (ADT)?

2. Floating-Point Literals in Java

Here, datatypes can only be specified in decimal forms and not in octal or hexadecimal form.

  • Decimal (Base 10)
package com.dataflair.literals;

public class FloatingPointLiteral 
  public static void main(String args[])
    double decimalValue = 101.230; // decimal-form literal
    double decimalValue1 = 0123.222; // It also acts as decimal literal
    double hexaDecimalValue = 1.234e2; // Hexa-decimal form
    System.out.println("Decimal form literal is "+decimalValue);
    System.out.println("Second Decimal form literal is "+decimalValue1);
    System.out.println("Hexa decimal form literal is "+hexaDecimalValue);

Output –Floating-point-literal-in-java

Every floating type is a double type and this the reason why we cannot assign it directly to float variable, to escape this situation we use f or F as suffix, and for double we use d or D.

Have you checked the Latest Career Opportunities of Java?

3. Char Literals in Java

These are the four types of char-

Char Literals in Java with rela-time examples

  • Single Quote

Java Literal can be specified to a char data type as a single character within a single quote.

char ch = 'a';
  • Char as Integral 

A char literal in Java can specify as integral literal which also represents the Unicode value of a character.
Furthermore, an integer can specify in decimal, octal and even hexadecimal type, but the range is 0-65535.

char ch = 062;
  • Unicode Representation

Char literals can specify in Unicode representation ‘\uxxxx’. Here XXXX represents 4 hexadecimal numbers.

char ch = '\u0061';// Here /u0061 represent a.
  • Escape Sequence

Escape sequences can also specify as char literal.

char ch = '\n';


package com.dataflair.literals;

public class CharacterLiteral {
  public static void main(String[] args)
    char character = 'd';
    //char number = 0789; error: Integer number too large
    char unicodeCharacter = '\u0064';
    System.out.println("\"  is a symbol");

Output –

4. String Literals

Java String literals are any sequence of characters with a double quote.

String s = "Hello";

They may not contain unescaped newline or linefeed characters.

However, the Java compiler will evaluate compile-time expressions.


package com.dataflair.literals;

public class StringLiteral {
  public static void main(String[] args) 
    String myString  = "Hello! Welcome to DataFlair"; 

    // If we assign without "" then it treats as a variable 
    // and causes compiler error 
    // String myString1 = Hello;  

    System.out.println(myString ); 



Output –String-literal-program-in-java

5. Boolean Literals

They allow only two values i.e. true and false.

boolean b = true;

Example –

package com.dataflair.literals;

public class BooleanLiteral {
  public static void main(String[] args)
    boolean boolVar1 = true;
    boolean boolVar2 = false;
    // boolean boolVar3 = 0; error: incompatible types: int cannot be converted to boolean
    // boolean boolVar1 = 1; error: incompatible types: int cannot be converted to boolean

Output –


Literals in Java are an important concept, which will help you to build your basics in programming. Now, you can implement 5 types of literals in your program.

Just practice and implement!!

Nourish your fundamentals with Operators in Java

1 Response

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.