Literals in Java

In Java, a literal is a fixed value that represents itself and is directly used in the code. Literals are used either to assign specific values to variables or to represent constant values within the code.

There are several types of literals that represent different values. Here are some examples of literals in Java:

  1. Integer Literals: Integer literals represent whole numbers without a decimal point. They can be written in the following formats:
  2. FormatDescriptionExample
    DecimalDecimal is the base-10 number system that we use every day for calculations. It consists of the numbers from 0 to 9.
    15
    HexadecimalThe numeral system that is a made up of 16 symbols, 0 through 9 and the letters A through F. It is a base 16 numerical system. It is often shortened to hex.
    0xFF
    BinaryThe numeral system that is a made up of 2 symbols (0 and 1). It is a base 2 numerical system. The binary literals can be created in Java SE 7 and later.
    0b11010

    Example:

    int a = 15;      // Decimal literal
    int b = 0b1010;  // Binary literal (0b or 0B prefix)
    int c = 012;     // Octal literal (0 prefix)
    int d = 0xA;     // Hexadecimal literal (0x or 0X prefix)

    An integer literal that ends with the letter L or l is of type long otherwise it is of type int. It is recommended to use the upper case letter L because the lower case letter l is hard to distinguish from the digit 1. For example: 10000004543L

  3. Floating-Point Literals: Floating-point literals represent decimal numbers either with a decimal point or using scientific notation. They can be expressed in two formats: as a decimal (with or without an exponent) or in scientific notation (using exponential form).
  4. Example:

    double e = 3.14;     // Decimal literal
    double f = 3.0e2;    // Scientific notation (3.0 * 10^2)

    A float value that ends with the letter F or f is a floating-point literal, otherwise it is of double type. The double literal can optionally end with the letter D or d (by convention, it is omitted). For scientific notation, the floating point types (float and double) can also be expressed using E or e.

  5. Character Literals: Character literals represent individual characters enclosed in single quotes. They can be either actual characters or escape sequences. To assign char literals, use 'single quotes'. Char literals may include any Unicode (UTF-16) characters.
  6. Example:

    char g = 'A';      // Character literal
    char h = '\n';     // Newline character
    char i = '\u0041'; // Unicode escape sequence (for 'A')
  7. String Literals: String literals represent sequences of characters enclosed in double quotes. They are used to represent text in Java. To assign String literals use "double quotes". String Literals may include any Unicode (UTF-16) characters.
  8. Example:

    String message = "Hello, World!";
  9. Boolean Literals: Boolean literals represent the two boolean values: true and false. They are used for logical operations and conditions.
  10. Example:

    boolean a = true;
    boolean b = false;
  11. Null Literal: The null literal represents the absence of a value or a null reference. It is a special value that can be assigned to any object reference type to indicate that it does not refer to any object.
  12. Example:

    String str = null; // Assigning null to a String reference
    Object obj = null; // Assigning null to an Object reference
    Integer number = null; // Assigning null to an Integer reference (Integer is an object wrapper class for int)
  13. Class Literal: The class literal is formed by using a type name, followed by .class. For example:
  14. Example:

    Class a = String.class;
    Class b = long.class;

These literals are used to directly specify values of different data types in Java. They help in expressing specific values without the need for variables or computations.

Why are Literals Important?

  • By using literals, you can directly use the intended value without any intermediate variables or computations, making the code more readable.
  • Directly using literals can sometimes lead to more optimized bytecode, especially when compared to using variables that hold the same constant value.
  • Using literals as constants, ensure that the value remains unchanged throughout the program, which can be useful for certain applications and algorithms.

Best Practices

  • Consistency: Ensure consistency in the use of literals throughout the codebase. If a value is a constant, consider using a named constant (using the final keyword) instead of repeating literals.
  • Clarity over Conciseness: While it's tempting to use literals directly, always prioritize code clarity. If a literal's meaning isn't immediately evident, consider using a named constant or a well-named variable instead.

Underscore Characters in Numeric Literals

Since Java 7, underscore characters _ can be inserted between digits in a numerical literal. The use of underscore between numbers may help to improve the readability of a code by separating groups of digits in numeric literals. For example:

long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi = 3.14_15F;

We can place underscores only between digits; we cannot place underscores in the following places:

  • Underscores cannot be inserted adjacent to a decimal point in a floating point literal. For example: float pi = 3._1415F; is invalid.
  • Underscores cannot be placed prior to an F or L suffix. For example: long socialSecurityNumber = 999_99_9999_L; is invalid.
  • Underscores cannot be placed in positions where a string of digits is expected.