Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaLiterals in Java

Literals in Java

Literal: Any constant value which can be assigned to the variable is called literal/constant. 

In simple words, Literals in Java is a synthetic representation of boolean, numeric, character, or string data. It is a medium of expressing particular values in the program, such as an integer variable named ‘’/count is assigned an integer value in the following statement.

// Here 100 is a constant/literal.
int x = 100; 

Integral literals

For Integral data types (byte, short, int, long), we can specify literals in 4 ways:- 

Decimal literals (Base 10): In this form, the allowed digits are 0-9.

int x = 101;

Octal literals (Base 8): In this form, the allowed digits are 0-7.

// The octal number should be prefix with 0.
int x = 0146; 

Hexa-decimal literals (Base 16): In this form, the allowed digits are 0-9, and characters are a-f. We can use both uppercase and lowercase characters as we know that java is a case-sensitive programming language, but here java is not case-sensitive.

// The hexa-decimal number should be prefix
// with 0X or 0x.
int x = 0X123Face; 

Binary literals: From 1.7 onward, we can specify literal value even in binary form also, allowed digits are 0 and 1. Literals value should be prefixed with 0b or 0B.

int x = 0b1111;

Example: 

Java




// Java program to illustrate the
// application of Integer literals
 
public class Test {
    public static void main(String[] args)
    {
          // decimal-form literal
        int a = 101;
          // octal-form literal
        int b = 0100;
        // Hexa-decimal form literal
        int c = 0xFace;
          // Binary literal
        int d = 0b1111;
       
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
    }
}


Output

101
64
64206
15

Note : By default, every literal is of int type, we can specify explicitly as long type by suffixed with l or L. There is no way to specify byte and short literals explicitly but indirectly we can specify. Whenever we are assigning integral literal to the byte variable and if the value is within the range of byte then the compiler treats it automatically as byte literals.

Floating-Point literal

For Floating-point data types, we can specify literals in only decimal form, and we cant specify in octal and Hexadecimal forms. 

Decimal literals(Base 10): In this form, the allowed digits are 0-9. 

double d = 123.456;

Java




// Java program to illustrate the 
// application of floating-point literals
 
public class Test {
    public static void main(String[] args)
    {
          // decimal-form literal
        float a = 101.230f;
          // It also acts as decimal literal
        float b = 0123.222f;
          // Hexa-decimal form (error)
        float c = 0x123.222;
       
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }
}


Output

101.230
123.222
Error: malformed floating point literal

Note: By default, every floating-point literal is of double type, and hence we cant assign directly to the float variable. But we can specify floating-point literal as float type by suffixed with f or F. We can specify explicitly floating-point literal as double type by suffixed with d or D. Of course this convention is not required.
 

Char literals

For char data types, we can specify literals in 4 ways: 

Single quote: We can specify literal to a char data type as a single character within the single quote.

char ch = 'a';

Char literal as Integral literal: we can specify char literal as integral literal, which represents the Unicode value of the character, and that integral literal can be specified either in Decimal, Octal, and Hexadecimal forms. But the allowed range is 0 to 65535.

char ch = 062;

Unicode Representation: We can specify char literals in Unicode representation ‘\uxxxx’. Here xxxx represents 4 hexadecimal numbers.

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

Escape Sequence: Every escape character can be specified as char literals.

char ch = '\n';

Example:

Java




// Java program to illustrate the
// application of char literals
 
public class Test {
    public static void main(String[] args)
    {
          // single character literal within single quote
        char ch = 'a';
          // It is an Integer literal with octal form
        char b = 0789;
          // Unicode representation
        char c = '\u0061';
       
        System.out.println(ch);
        System.out.println(b);
        System.out.println(c);
       
        // Escape character literal
        System.out.println("\"  is a symbol");
    }
}


Output

a
error:Integer number too large
a
"  is a symbol

String literals

Any sequence of characters within double quotes is treated as String literals. 

String s = "Hello";

String literals may not contain unescaped newline or linefeed characters. However, the Java compiler will evaluate compile-time expressions, so the following String expression results in a string with three lines of text: 

Example:
String text = "This is a String literal\n"
            + "which spans not one and not two\n"
            + "but three lines of text.\n";

Java




// Java program to illustrate the
// application of String literals
 
public class Test {
    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;
 
        System.out.println(s);
        System.out.println(s1);
    }
}


Output

Hello
error: cannot find symbol
symbol:   variable Hello
location: class Test

Boolean literals

Only two values are allowed for Boolean literals, i.e., true and false. 

boolean b = true;

Java




// Java program to illustrate the
// application of boolean literals
 
public class Test {
    public static void main(String[] args)
    {
        boolean b = true;
        boolean c = false;
        boolean d = 0;
        boolean b = 1;
 
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        System.out.println(e);
    }
}


Output

true
false
error: incompatible types: int cannot be converted to boolean
error: incompatible types: int cannot be converted to boolean

Note: When we are performing concatenation operations, then the values in brackets are concatenated first. Then the values are concatenated from the left to the right. We should be careful when we are mixing character literals and integers in String concatenation operations and this type of operation are known as Mixed Mode operation.

Java




// Java program to illustrate the behaviour of
// char literals and integer literals when
// we are performing addition
 
public class Test {
    public static void main(String[] args)
    {
        // ASCII value of 0 is 48
        int first = '0';
 
        // ASCII value of 7 is 55
        int second = '7';
        System.out.println("Geeks!" + first +
                                '2' + second);
    }
}


Output

Geeks!48255

Explanation: Whenever we are performing addition between a string and integer, the overall result is converted into a string. The above program evaluation is done in the following way:  

"Geeks!" + first + '2' + second
"Geeks! " + 48 + '2' + 55
"Geeks!48" + '2' + 55
"Geeks!482" + 55
"Geeks!48255"

This article is contributed by Bishal Kumar Dubey. If you like Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks. Please write comments if you find anything incorrect or you want to share more information about the topic discussed above.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments