Advanced Literals and Constant Values in Java

In Java, literals are fixed values assigned directly to variables.

They represent constant data that doesn’t change during program execution.

For example:

int a = 10;

Here, 10 is a literal — a constant value assigned to the variable a.

Java supports several types of literals such as integer, floating-point, boolean, character, and the null literal.

1. Integer Literals

Integer literals represent whole number values without decimal points. They can be written in four different number systems:

Number System Prefix Example Description
Decimal None int a = 10; Regular base-10 number system
Binary 0b or 0B int b = 0b1010; Represents binary value (2-based)
Octal 0 int c = 012; Represents octal (8-based) number
Hexadecimal 0x or 0X int d = 0xA; Represents hexadecimal (16-based) number

Example:

public class IntegerLiterals {
    public static void main(String[] args) {
        int decimal = 10;
        int binary = 0b1010;
        int octal = 012;
        int hex = 0xA;

        System.out.println("Decimal: " + decimal);
        System.out.println("Binary: " + binary);
        System.out.println("Octal: " + octal);
        System.out.println("Hexadecimal: " + hex);
    }
}

Output:

Decimal: 10

Binary: 10

Octal: 10

Hexadecimal: 10

Explanation:

This program demonstrates how Java represents integer literals in different number systems:

  • Decimal (base 10): Written normally without any prefix, e.g., 10.
  • Binary (base 2): Starts with 0b or 0B, e.g., 0b1010 (which equals 10 in decimal).
  • Octal (base 8): Starts with 0, e.g., 012 (1×8 + 2 = 10 in decimal).
  • Hexadecimal (base 16): Starts with 0x or 0X, e.g., 0xA (A represents 10 in decimal).

2. Floating-Point Literals

Floating-point literals represent decimal numbers or numbers with a fractional part. They can be written in two forms:

Type Example Description
Decimal form double num = 12.34; Normal decimal number
Exponential form (Scientific notation) float val = 1.23e3f; Represents 1.23 × 10³ = 1230.0

By default, Java treats all floating-point numbers as double. To specify a float, you must add the suffix ‘f’ or ‘F’.

Example:

public class FloatingLiterals {
    public static void main(String[] args) {
        float f = 12.5f;
        double d = 123.456;
        double sci = 1.2e3; // 1.2 × 10³ = 1200.0

        System.out.println("Float value: " + f);
        System.out.println("Double value: " + d);
        System.out.println("Scientific notation: " + sci);
    }
}

Output:

Float value: 12.5

Double value: 123.456

Scientific notation: 1200.0

Explanation:

This program shows how floating-point literals work in Java:

  • float f = 12.5f; → The suffix f (or F) is required to tell Java that the number is a float type.
  • double d = 123.456; → By default, any decimal number in Java is treated as a double unless specified otherwise.
  • double sci = 1.2e3; → The scientific notation 1.2e3 means 1.2 × 10³ = 1200.0.

So, the program prints the values of float, double, and a double written in scientific form, showing that Java supports decimal and exponential notations for real numbers.

3. Boolean Literals

Boolean literals represent truth values — either true or false.

They are used for conditional checks and logical operations.

Example:

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

        System.out.println("Is Java fun? " + isJavaFun);
        System.out.println("Is Java easy? " + isEasy);
    }
}

Output:

Is Java fun? true

Is Java easy? false

Explanation:

This program demonstrates the use of Boolean literals in Java.

  • A boolean variable can hold only two values true or false.
  • isJavaFun = true means the condition is true, while isEasy = false means the condition is false.
  • When printed, the output directly displays these logical values.

Boolean literals are mainly used in conditional statements like if, while, and logical operations to control the flow of a program.

4. Character Literals

Character literals represent a single character enclosed in single quotes (' '). They can also use escape sequences or Unicode representations.

(a) Normal Character

char ch = 'A';

(b) Escape Sequences

Escape sequences are special symbols that represent non-printable or formatted characters.

Escape Sequence Meaning
\n New line
\t Tab space
\' Single quote
\" Double quote
\\ Backslash

Example:

public class CharacterLiterals {
    public static void main(String[] args) {
        char newline = '\n';
        char tab = '\t';
        char quote = '\"';
        System.out.println("Hello" + newline + "World");
        System.out.println("Java" + tab + "Programming");
        System.out.println("Quote Example: " + quote + "Learn Java" + quote);
    }
}

Output:

Hello

World

Java Programming

Quote Example: "Learn Java"

Explanation:

This program demonstrates the use of Character literals and escape sequences in Java.

  • A character literal is a single character enclosed in single quotes (' ').
  • Some characters cannot be typed directly (like newline, tab, or quotes), so escape sequences are used:
    • '\n' → Inserts a new line.
    • '\t' → Inserts a tab space.
    • '\"' → Prints a double quote.

The program uses these escape sequences to format the output neatly:

  • "Hello" and "World" appear on separate lines.
  • "Java" and "Programming" are separated by a tab space.
  • The last line prints quotes around the text "Learn Java".

(c) Unicode Characters

Unicode allows Java to represent characters from any language in the world using the format '\uXXXX'.

Example:

public class UnicodeExample {
    public static void main(String[] args) {
        char symbol = '\u2764'; // Unicode for heart (♥)
        System.out.println("Unicode Character: " + symbol);
    }
}

Output:

Unicode Character: ♥

Explanation:

This program demonstrates the use of Unicode literals in Java.

  • In Java, every character is internally represented using Unicode, which allows you to display symbols and characters from different languages.
  • A Unicode literal is written as '\uXXXX', where XXXX is a 4-digit hexadecimal code.
  • In this example:
  • '\u2764' represents the heart symbol (♥).
  • When the program runs, it prints the corresponding Unicode character on the screen.

5. null Literal

The null literal represents a reference with no value assigned. It is used with non-primitive types (objects), not with primitives.

Example:

public class NullLiteral {
    public static void main(String[] args) {
        String name = null;
        System.out.println("Value of name: " + name);
    }
}

Output:

Value of name: null

Explanation:

This example demonstrates the null literal in Java.

  • null represents the absence of a value or no reference to any object.
  • It can only be assigned to reference data types (like String, Array, Object, etc.), not to primitive types.
  • In this program:
    • The variable name is declared as a String but assigned null, meaning it doesn’t point to any object in memory.
  • When printed, Java displays the word "null" to indicate that the reference is empty.

Summary:

Literal Type Description Example
Integer Whole numbers (decimal, binary, octal, hex) int a = 0xF;
Floating-point Numbers with decimals or scientific notation double d = 12.5;
Boolean Logical values boolean b = true;
Character Single characters, escape sequences, Unicode char c = '\u0041';
null Reference literal with no assigned object String s = null;

Conclusion

  • Java provides special literals to represent constant values of different data types.
  • These literals make code more readable, compact, and expressive.
  • Understanding them is crucial for type handling, memory efficiency, and error-free programming.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar