Java.Core.What are literals?

Literals in Java are fixed values assigned to variables or used directly in expressions. They represent constant values of different types, such as numbers, characters, booleans, and strings.


Types of Literals in Java

Java has the following main types of literals:

  1. Integer Literals (int, long)
  2. Floating-Point Literals (float, double)
  3. Character Literals (char)
  4. String Literals (String)
  5. Boolean Literals (true, false)
  6. Null Literal (null)

1. Integer Literals

Integer literals represent whole numbers and can be expressed in different number systems:

  • Decimal (Base 10) (default)
  • Binary (Base 2) (0b prefix)
  • Octal (Base 8) (0 prefix)
  • Hexadecimal (Base 16) (0x prefix)

Example:

public class IntegerLiterals {
    public static void main(String[] args) {
        int decimal = 42;      // Decimal (Base 10)
        int binary = 0b101010; // Binary (Base 2)
        int octal = 052;       // Octal (Base 8)
        int hex = 0x2A;        // Hexadecimal (Base 16)

        long bigNumber = 1_000_000_000L; // Underscores for readability (Java 7+), 'L' for long

        System.out.println(decimal); // 42
        System.out.println(binary);  // 42
        System.out.println(octal);   // 42
        System.out.println(hex);     // 42
        System.out.println(bigNumber); // 1000000000
    }
}

👉 Notes:

  • Underscores (_) can be used for better readability.
  • Suffix L is required for long literals (uppercase L is preferred over lowercase l to avoid confusion with 1).

2. Floating-Point Literals

Floating-point literals represent decimal numbers and can be of type:

  • float (suffix F or f)
  • double (default, suffix D or d is optional)

Example:

public class FloatLiterals {
    public static void main(String[] args) {
        double pi = 3.14159;  // Default type is double
        float g = 9.81F;      // 'F' required for float
        double scientific = 1.23e3; // Scientific notation (1.23 × 10³)

        System.out.println(pi); // 3.14159
        System.out.println(g);  // 9.81
        System.out.println(scientific); // 1230.0
    }
}

👉 Notes:

  • Scientific notation (e or E) can be used for large numbers.
  • Use F for float literals to avoid implicit conversion to double.

3. Character Literals

Character literals represent a single character enclosed in single quotes ' '.

Example:

public class CharLiterals {
    public static void main(String[] args) {
        char letter = 'A';
        char digit = '9';
        char special = '$';

        // Unicode character
        char unicodeChar = '\u2764'; // Unicode for ❤️

        System.out.println(letter); // A
        System.out.println(digit);  // 9
        System.out.println(special); // $
        System.out.println(unicodeChar); // ❤️
    }
}

👉 Escape sequences (like \n, \t, \b) can be used in character literals.


4. String Literals

String literals represent a sequence of characters enclosed in double quotes " ".

Example:

public class StringLiterals {
    public static void main(String[] args) {
        String greeting = "Hello, World!";
        String multiline = """
                This is a multi-line
                text block in Java 15+.
                """;

        System.out.println(greeting);
        System.out.println(multiline);
    }
}

👉 Notes:

  • Strings are immutable in Java.
  • Java 15+ introduced text blocks (""") for multi-line strings.

5. Boolean Literals

Boolean literals represent true/false values.

Example:

public class BooleanLiterals {
    public static void main(String[] args) {
        boolean isJavaFun = true;
        boolean isCold = false;

        System.out.println(isJavaFun); // true
        System.out.println(isCold); // false
    }
}

👉 Notes:

  • Only true or false are valid boolean literals.
  • Booleans cannot be converted to numbers like in some other languages.

6. Null Literal

The null literal represents the absence of a value for object references.

Example:

public class NullLiteral {
    public static void main(String[] args) {
        String text = null;
        System.out.println(text); // Output: null
    }
}

👉 Notes:

  • null is used with reference types, not primitives.

Summary Table

TypeExample LiteralsNotes
Integer42, 0b1010, 052, 0x1ABinary (0b), Octal (0), Hex (0x)
Floating-Point3.14, 2.5F, 6.02e23F for float, D for double
Character'A', '\n', '\u2764'Unicode (\u), Escape sequences (\n)
String"Hello", """Multiline"""String objects, text blocks (""")
Booleantrue, falseOnly true or false allowed
NullnullRepresents absence of an object

Conclusion

  • Literals are constant values assigned to variables.
  • Java supports integer, floating-point, character, string, boolean, and null literals.
  • Use proper suffixes (L, F, D) for better clarity.
  • Use escape sequences in character literals ('\n', '\t').
  • Use text blocks (""") for multi-line strings in Java 15+.
This entry was posted in Без рубрики. Bookmark the permalink.