Literals in Java

Literals in Java with Examples

In this article, I am going to discuss the Literals in Java with examples. Please read our previous article, where we discussed Data Types in Java in detail. At the end of this article, you will understand what are literals and when and how to use Literals in Java Application.

What are Literals in Java?

A constant value in Java is created by using a literal representation of it. Java Literals are syntactic representations of boolean, character, numeric, or string data. Literals provide a means of expressing specific values in your program. In other words, Literals in Java are the constant values assigned to the variable. It is also called a constant.

Example: int i = 100; Here, 100 is a literal.

Types of Literals in Java:

Basically there are five types of literals in Java :

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

Types of Literals in Java

Integer Literals in Java:

Integers are probably the most commonly used type in the typical program. Any whole number value is an integer literal. The integer literal can be specified in four ways :

Decimal (Base-10): Digits from 0-9 are allowed in this form. Example: int x=101;

Octal (Base-8): Digits from 0 – 7 are allowed. It should always have a prefix 0. Example: int x=0146;

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. Example: 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. Example: int x = 0b1111;

Sample Program for Integer Literal:
package Demo;

public class IntegerLiteral {

    public static void main(String[] args) {
            int a = 101; // decimal literal 
            int b = 0146; // octal literal 
            int c = 0x123Face; // Hexa-decimal literal 
            int d = 0b1111; // Binary literal 
            System.out.println(a); 
            System.out.println(b); 
            System.out.println(c); 
            System.out.println(d); 

    }
}

Output:

Integer Literals in Java

Floating-Point Literals in Java:

Floating-point numbers represent decimal values with a fractional component. For Floating-point data types, we can specify literals in only decimal form and we can’t specify in Octal and Hexadecimal forms.

Decimal (Base-10): In this form, the allowed digits are 0-9. Example: double d = 123.456;

Sample program for Floating-Point Literal:

package Demo;

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 Literals in Java

Boolean Literal in Java:

Boolean literals are simple. There are only two logical values that a boolean value can have, true and false. The values of true and false do not convert into any numerical representation. The true literal in Java does not equal 1, nor does the false literal equal 0. Example: boolean b = true;

Sample program for Boolean Literal:

package Demo;

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
         System.out.println(boolVar1);
         System.out.println(boolVar2);
    }

}

Output:

Boolean Literal in Java

Character Literal in Java:

A literal character is represented inside a pair of single quotes. There are four types of character literals:

Single-Quote: Java Literal can be specified to a char data type as a single character within a single quote. Example: char ch = ‘a’;

Char as Integral: A char literal in Java can specify an integral literal which also represents the Unicode value of a character. Example: char ch = 062;

Unicode Representation: Char literals can specify in Unicode representation ‘\uxxxx’. Here XXXX represents 4 hexadecimal numbers. Example: char ch = ‘\u0061’; // Here 0061 represent a.

Escape Sequence: Escape sequences can also specify as char literal. Example: char ch = ‘\n’;

Sample Program for Character Literal:

package Demo;

public class CharacterLiteral {

    public static void main(String[] args) {
         	char ch = 'a'; // signle character literl within signle quote 
           // char b = 0789;  It is an Integer literal with octal form 
         	//error : Integer value too large
            char c = '\u0061'; // Unicode representation 
            System.out.println(ch);  
            System.out.println(c); 
            // Escape character literal 
            System.out.println("\"  is a symbol"); 

    }

}

Output:

Character Literal in Java:

String Literals in Java:

String Literals in Java are specified like they are in most other languages – by enclosing a sequence of characters between a pair of double-quotes. Example: String s = “Hello”;

Sample Program for String Literal:

package Demo;

public class BooleanLitera {

    public static void main(String[] args) {
        String s = "Hello"; 
          
          // If we assign without "" then it treats as a variable 
          // and causes compiler error 
          // String s1 = Hello;  It gives an error
  
          System.out.println(s); 

    }
}

Output:

String Literals

In the next article, I am going to discuss Type Casting in Java with examples. Here, in this article, I try to explain Literals in Java with some examples. I hope you enjoy this article.

Leave a Reply

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