OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 1 : Literal constants and Symbolic constants
What is a Constant?
A constant is a value that does not change during the execution of a program. It remains fixed.
Java supports two main types of constants:
1. Literal Constants
2. Symbolic Constants
1. Literal Constants
Definition:
Literal constants are the actual fixed values directly used in Java code. They are also called literals.
Key Point:
They are raw values written directly in the program and used without a name.
Types of Literal Constants:
Type
Description
Example
Integer Literal
Whole numbers without decimal. Can be in decimal, octal, or hexadecimal.
10
, -99
, 0x1A
, 075
Floating Literal
Numbers with decimal or exponent.
3.14
, 1.2e3
, -0.5
Character Literal
Single character enclosed in single quotes.
'A'
, '9'
, '#'
String Literal
Group of characters enclosed in double quotes.
"Java"
, "123"
, " "
Boolean Literal
Logical constants for decision-making.
true
, false
Null Literal
Special literal that represents "no object".
null
Type | Description | Example |
---|---|---|
Integer Literal | Whole numbers without decimal. Can be in decimal, octal, or hexadecimal. |
|
Floating Literal | Numbers with decimal or exponent. |
|
Character Literal | Single character enclosed in single quotes. |
|
String Literal | Group of characters enclosed in double quotes. |
|
Boolean Literal | Logical constants for decision-making. |
|
Null Literal | Special literal that represents "no object". |
|
Examples of Literal Constants:
2. Symbolic Constants
Definition:
A symbolic constant is a named constant declared using the final
keyword, which makes the value unchangeable after assignment.
Key Point:
It gives a name (symbol) to a value that doesn’t change – increasing readability and reusability.
🔸 Declaration Syntax:
🔸 Example:
🔸 Usage in Program:
🔸 Difference Between Literal and Symbolic Constants
Feature Literal Constant Symbolic Constant What it is Fixed value written directly Fixed value assigned to a named variable Keyword used None final
Readability Lower in complex code Higher due to meaningful names Reusability Not reusable Reusable throughout the code Best Use Case For single-use values For values used multiple times Example 3.14
, "Hello"
final double PI = 3.14;
Feature | Literal Constant | Symbolic Constant |
---|---|---|
What it is | Fixed value written directly | Fixed value assigned to a named variable |
Keyword used | None | final |
Readability | Lower in complex code | Higher due to meaningful names |
Reusability | Not reusable | Reusable throughout the code |
Best Use Case | For single-use values | For values used multiple times |
Example | 3.14 , "Hello" | final double PI = 3.14; |
Naming Convention for Symbolic Constants
Use all uppercase letters.
Use underscores to separate words.
✅ Examples:
✅ Advantages of Symbolic Constants
✔ Improved readability of code.✔ Avoids magic numbers (hard-coded values).✔ Easier to update values in one place.✔ Prevents accidental changes (value can't be modified).
Comments
Post a Comment