Lecture - 3rd What are Java Literals


What is a Literal ?

A constant value that is assigned to a variable that is called a literal.
for eg:

           int number =10;                                    (Integer Literal)
           float number = 10.1F;                           (Float Literal)
           double number = 10.234;                      (Double Literal (By Default))
           char character = 'a' or 'A';                     (Character Literal)
           long number = 2345245;                       (Long Literal)
           boolean result = true/false;                   (Boolean Literal)
           short number = 23;                                (Short Literal)
           byte number = 234;                               (Byte Literal)
             String name = "Sachin Malhotra";         (String Literal)

In order to choose the data type we first need to find the type of data we want to store after that we need to analyze the minimum and maximum values.

Keywords in Java:

"Java has some reserved keywords that can't be named for a variable few examples are as follows:"

for more information about please go to Java Documentation - Get Started (oracle.com)

Please click the link for download the note PDF of this topic : Notes PDF

                                                                                                                        Next Page>>
Code Snippet for Reference


// video number -4 ie java literals

package javaliterals;

public class JavaLiterals {

    public static void main(String[] args) {
       
        // age = variable name
        
        // byte literal = 22
        // range of byte = -128 to +127
        byte age = 12;
        
        // short literal can take upto 4 digits
        short value = 3490;
        
        // long can take upto 9 digit without L 
        long value2 = 6789456789090909009L;
        
        // integer literal can take input upto 9 digit
        int value3 =  999999999;
        
        // character literal
        char name = 'A';
        
        //boolean literal default value is false
        boolean result = false;
        
        // float literal
        float kilometer = 23.2f;
        
        // double literal
        double kilometers = 34.788d;
        
        System.out.println("the byte literal is "+age);
        System.out.println("the short literal is "+value);
        System.out.println("the integer literal is "+value3);
        System.out.println("the long literal is "+value2);
        System.out.println("the boolean literal is "+result);
        System.out.println("the char literal is "+name);
        System.out.println("the float literal is "+kilometer); 
        System.out.println("the double literal is "+kilometers); 
        
    }
    
}