Literals in Java – Integral, Floating, Char, String, Boolean

1. Objective

In our last tutorial, we discussed JDK Vs JRE Vs JVM. Here, in this Java Literals tutorial, we are going to learn about what is literals in Java programming language. Moreover, we will discuss types of Java literals: integer, floating-point, char, string and boolean literals in Java with examples and many other things.

So, let’s start with Java literals tutorial.

Literals in Java

Literals in Java – Integral, Floating, Char, String, Boolean

2. Literals in Java

Java Literals are the constant values assigned to the variable. It is also called a constant.

Java Literals Example –

int x = 100;

Here, 100 is literal.

Do you know What is StringBuffer In Java?

3. Types of Java Literals

a. Integral Literals in Java

We can specify the integer literals in 4 different ways –

Integral Literals in Java

Integral Literals in Java

i. Decimal Literals (Base 10)

Digits from 0-9 are allowed in this form.

int x = 101;

Let’s Explore Java Syntax

ii. Octal Literals (Base 8)

Digits from 0 – 7 are allowed. It should always have a prefix 0.

int x = 0146;

iii. Hexa-Decimal Literals (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.

int x = 0X123Face;

iv. Binary Literals

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.

int x = 0b1111;

Read about Java Operators – Types of Operators in Java

Example-

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

We can specify explicitly as long type by suffixed with l or L but there is no way to specify byte and short, but if it’s in the range the compiler automatically treats it as a byte.

b. Floating-Point Literals in Java

Here, datatypes can only be specified in decimal forms and not in octal or hexadecimal form.

i. Decimal literals (Base 10)

public class Test {
    public static void main(String[] args)
    {
        int a = 101.230; // decimal-form literal
        int b = 0123.222; // It also acts as decimal literal
        int c = 0x123.222; // Hexa-decimal form
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }
}

Output –

101.230

123.222

Error: malformed floating point literal

Every floating type is a double type and this the reason why we cannot assign it directly to float variable, to escape this situation we use f or F as suffix, and for double we use d or D.

Let’s Discuss Different Types of Exceptions in Java Programming Language

c. Char Literals in Java

There are four ways in which we can specify char literals  –

Char Literals in Java

Char Literals in Java

i. Single Quote

Java Literal can be specified to char data type as a single character within a single quote.

char ch = 'a';

Do You Know Difference Between Checked and Unchecked Exceptions in Java

ii. Char Literal as Integral Literals in Java

A char literal in Java can specify as integral literal which also represents the Unicode value of a character.
Furthermore, integer literals in Java can specify in decimal, octal and even hexadecimal type, but the range is 0-65535.

char ch = 062;

iii. Unicode Representation

Char literals in Java can specify in Unicode representation ‘\uxxxx’. Here XXXX represents 4 hexadecimal numbers.

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

iv. Escape Sequence

Escape sequences can also specify as char literal.

char ch = '\n';

Let’s See How Java Handle Exceptions 
Example-

public class Test {
public static void main(String[] args)
    {
        char ch = 'a';
        char b = 0789;
        char c = '\u0061';
        System.out.println(ch);
        System.out.println(b);
        System.out.println(c);
        System.out.println("\"  is a symbol");
    }
}

Output –
a
error: Integer number too large
a
”  is a symbol

d. String Literals in Java

String literals in Java are any sequence of characters with a double quote.

String s = "Hello";

Java String literals may not contain unescaped newline or linefeed characters.

However, the Java compiler will evaluate compile-time expressions.

Read about Java Character Class Methods with Syntax and Examples

Example-

String text = "This is a String literal\n"
            + "which spans not one and not two\n"
            + "but three lines of text.\n";
public class Test {
    public static void main(String[] args)
    {
        String s = "Hello";
        String s1 = Hello;
        System.out.println(s);
        System.out.println(s1);
    }
}

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

e. Boolean Literals in Java

Java Boolean literals allow only two values i.e. true and false.

boolean b = true;

Example –

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

Let’s Look at Java Number Methods with Syntax and Examples

public class Test {
    public static void main(String[] args)
    {
        int first = '0';
        int second = '7';
        System.out.println("Shadow!" + first +
                                '2' + second);
    }
}

Output-
Shadow!48255

Explanation –
Whenever we are performing addition between a string and integer then the overall result is converted into a string.
“Shadow!” + first + ‘2’ + second
“Shadow! ” + 48 + ‘2’ + 55
“Shadow!48” + ‘2’ + 55
“Shadow!482” + 55
“Shadow!48255”

This was all about Java Literals Tutorial. Hope you like our explanation.

4. Conclusion

Hence, in this Java tutorial, we learned about what are literals in Java and different types of Java Literals: integral, floating-point, char, string, and boolean literals in Java. In addition, we also discuss subtypes of Integral literals: decimal, octal, hexa-decimal, and binary literals and char literals in Java: single quote, char literal as Integral, unicode representation, and escape sequence. Furthermore, if you have any query, feel free to ask in a comment section.

Related Topic- Java Annotations 

For reference 

Leave a Reply

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