L020 | Literals in Java | Class 9th | ICSE | Computer Applications | Java
What are Literals?
- Definition: Literals are fixed values assigned to variables in a program. They represent constant data directly written into the code and do not change during execution.
- Purpose: Literals are used to initialize variables or represent constant values in a program.
For example:
int age = 25; // Here, 25 is a literal.
Why Are Literals Important?
- They are essential for assigning values to variables.
- They simplify the representation of constant data like numbers, text, or boolean values in Java programs.
Types of Literals in Java
Java provides several types of literals to represent different kinds of data. Below are the main types of literals, discussed as per the ICSE syllabus.
1. Integer Literals
- Definition: Integer literals are used to represent whole numbers (without decimal points).
- Examples:
10, 25, 0.
Types of Integer Literals:
- Decimal (Base 10): Numbers in the standard numeric system.
- Octal (Base 8): Numbers starting with
0.
- Example:
int x = 075; // Octal for 61
- Hexadecimal (Base 16): Numbers starting with
0x or 0X.
- Example:
int x = 0x1F; // Hexadecimal for 31
- Binary (Base 2): Numbers starting with
0b or 0B.
- Example:
int x = 0b1010; // Binary for 10
2. Floating-Point Literals