Java Literals


Java Literals are syntactic representations of Boolean, numeric, character or string data.
Any constant value which can be assigned to the variable is called as literal/constant.
Example: int num = 100; //here 100 is literal/constant
Types of java Literals
1. Integer Literals
2. Floating-point Literals
3. Boolean Literals
4. Character Literals
5. String Literals
1. Integer Literals
For Integer data types (byte, int, short, long), we can specify literals in four ways:
i. Decimal literals (base 10)
In this form, the allowed digits are 0-9.
Example: int num = 123;
ii. Octal literals (base 8)
In this form, the allowed digits are 0-7.
iii. Hexa-decimal literals (base 16)
In this form, the allowed digits are 0-9 and characters are A-F. We can use both Upper-case/lower-case characters.
Note: The hexa-decimal number should be prefix with 0x/0X.
Example: int num = 0X123Face;
iv. Binary literals
From 1.7 onward we can specify literals value even in binary form also, allowed digits are 0 and 1. Literals value should be prefix with 0b/0B.
Example: int num = 0b00111;
/*
Java program to illustrate the application of Integer literals.
Date: Saturaday, 04-04-2020
@author: Ankur Saxena
Platform: Linux Ubuntu 12.04 (32-bit)/Jdk 8/Brackets text editor
*/

public class IntegerLiteralDemo{
     public static void main (String[] args){
          int num1 = 123; //decimal literal
          int num2 = 0123; //octal literal
          int num3 = 0x123Face; //hexa-decimal literal
          int num4 = 0b00111; //binary literal

          System.out.println (num1);
          System.out.println (num2);
          System.out.println (num3);
          System.out.println (num4);
     }
}
Save this file as ‘IntegerLiteralDemo.java’
Compile: $javac IntegerLiteralDemo.java
Execute: $java IntegerLiteralDemo
Output:

123
83
19135182
7

2. Floating-point Literals
For floating-point data types, we can specify literals in only decimal form and we cannot specify in octal and hexa-decimal forms.
--> Decimal literals (base 10)
In this form, the allowed digits are 0-9.
Example: double num = 123. 456;

/*
Java program to illustrate the application of floating-point literals.
Date: Saturaday, 04-04-2020
@author: Ankur Saxena
Platform: Linux Ubuntu 12.04 (32-bit)/Jdk 8/Brackets text editor
*/

public class FloatingPointLiteralDemo{
     public static void main (String[] args){
          double num1 = 122.111; //decimal literal
          double num2 = 0123.456; /*it also acts as decimal literal*/
          double num3 = 0x111.222; //hexa-decimal literal
         
          System.out.println (num1);
          System.out.println (num2);
          System.out.println (num3);
}
}

Save this file as ‘FloatingPointLiteralDemo.java’
Compile: $javac FloatingPointLiteralDemo.java
Execute: $java FloatingPointLiteralDemo
Output:

error: malformed floating point literal
/*
Java program to illustrate the application of floating-point literals.
Date: Saturaday, 04-04-2020
@author: Ankur Saxena
Platform: Linux Ubuntu 12.04 (32-bit)/Jdk 8/Brackets text editor
*/

public class FloatingPointLiteralDemo1{
     public static void main (String[] args){
          double num1 = 122.111; //decimal literal
          double num2 = 0123.456; /*it also acts as decimal literal*/
   //     double num3 = 0x111.222; //hexa-decimal literal
         
          System.out.println (num1);
          System.out.println (num2);
     //    System.out.println (num3);
    }
}

Save this file as ‘FloatingPointLiteralDemo1.java’
Compile: $javac FloatingPointLiteralDemo1.java
Execute: $java FloatingPointLiteralDemo1
Output:

122.111
123.456

3. Boolean Literals
In this form, only two values are allowed, i.e. true and false.
Example: boolean num = true;

/*
Java program to illustrate the application of Boolean Literals.
Date: Saturaday, 04-04-2020
@author: Ankur Saxena
Platform: Linux Ubuntu 12.04 (32-bit)/Jdk 8/Brackets text editor
*/
public class BooleanLiteralDemo{
     public static void main (String[] args){
          boolean num1 = true;
          boolean num2 = false;
          boolean num3 = 0;
          boolean num4 = 1;

          System.out.println (num1);
          System.out.println (num2);
          System.out.println (num3);
          System.out.println (num4);
}
}

Save this file as ‘BooleanLiteralDemo.java’
Compile: $javac BooleanLiteralDemo.java
Execute: $java BooleanLiteralDemo
Output:

error: incompatible types: int cannot be converted to boolean
  boolean num3 = 0;
error: incompatible types: int cannot be converted to boolean
  boolean num4 = 1;

/*
Java program to illustrate the application of Boolean Literals.
Date: Saturaday, 04-04-2020
@author: Ankur Saxena
Platform: Linux Ubuntu 12.04 (32-bit)/Jdk 8/Brackets text editor
*/
public class BooleanLiteralDemo1{
          public static void main (String[] args){
                   boolean num1 = true;
                   boolean num2 = false;
                    //boolean num3 = 0;
        //boolean num4 = 1;

                   System.out.println (num1);
                   System.out.println (num2);
                   //System.out.println (num3);
                   //System.out.println (num4);
  }
}

Save this file as ‘BooleanLiteralDemo1.java’
Compile: $javac BooleanLiteralDemo1.java
Execute: $java BooleanLiteralDemo1
Output:

true
false

4. Character Literals
For character data types, we can specify literals in four ways:
i. Single quote:
We can specify literals to char data type as single character within single quote.
Example: char ch = ‘a’;
ii. Character literal as Integer literal
We can specify character literal as integer literal which represents Unicode value of the character and that integer literals can be specified either in decimal, octal and hexa-decimal forms. But the allowed range is 0 to 65535.
Example: char ch = 0152;
iii. Unicode representation
We can specify character literals in Unicode representation ‘\uxxxx’. Here xxxx represents 4 gexa-decimal values.
Example: char ch = ‘u0041’;
iv. Escape sequence
Every escape sequence can be specified as the character literals.
Example: char ch = ‘\n’;
Escape               Meaning
\n                          new line
\t                           tab
\b                          backspace
\r                           carriage return
\f                           form feed
\\                           backslash
\’                           single quote
\”                           double quote
\d                          octal
\xd                        hexa-decimal
\ud                       Unicode character
5. String Literals
Any sequence of characters within double quotes is treated as the String Literals.
Example: String str = “Hello World!”


Java Program To Calculate And Display The Area Of A Circle