A literal represents a constant value which can be assigned to the variables


Integral Literals

    We can specify an integral literal in the following ways.

  1. Decimal literals
    allowed digits are 0 to 9
  2. int x = 10;
  3. Octal literals
    allowed digits are 0 to 7 but here literal value should be prefixed with 0(zero)
  4. int x = 010;
  5. Hexadecimal literals
    the allowed digits are 0 to 9, A- F (Both lower, Upper case) literals
    should be prefixed with 0x or oX
  6. int x = 0x10;


package com.java2learn.datatypes;

public class IntegralLiteralDemo {

	public static void main(String[] args) {
		int x = 10;
		int y = 010;
		int z = 0x10;
		System.out.println(x + "..." + y + "..." + z);


Except decimal, octal, hexadecimal there is no other way to represents constant values for the integral data type.

By default every integral lateral is of int datatype.We can specify explicitly an integral literal is of long type by suffixing with l or L


10 //int value.
10l //long value.
long l = 10l;
int i = 10l;//C.E:possible loss of precision 
 //found : long


There is no way to specify explicitly an integral literal is of type byte and short.If the integral literal is within the range of byte then the JVM by default treats it as byte literal.Similarly short literal also.

Floating point literals

  1. By default floating-point literals are double type we can specify explicitly as float type by suffixing with ‘f’ or ‘F’.
  2. float f = 10.5f;
  3. we can specify explicitly a floating point literal of double type by suffixing with d or D.we can also represent float-point literals by using scientific notation.
  4. double d = 10e23;
    int i = 10e23; //C.E:possible loss of precision
    //found : double
    //required : int.
  5. Floating point literals can be specified only in decimal form. i.e we can’t use octal and hexadecimal representation for floating point literals
  6. Double d = 0x123.456;
    Compile time error: Malformed floating-point literal.

Boolean Literals

  1. The only allowed values for boolean datatype are true, false.
  2. boolean b = true;

Character literals

  1. A char literal can be represented as a single character with in single quotes.
  2. char ch = 'a';
  3. we can represent a char literal by using it’s Unicode value. For char the allowed Unicode values are 0 to 65535.
  4. char ch = 97;
    System.out.println(ch); // Output: a
  5. we can represent a char literal by using Unicode representation which is nothing but ‘\uxxxx’
  6. char ch = '\u0061'
    System.out.println(ch); //Output:a
  7. we can also represent a char literal by using escape character.
  8. char ch = '\n';

The following is the list of all possible escape characters in java.

String literals

  1. A sequence of character with in double quotes is String literal.
  2. String s = "java2learn";