Exercises · Q4
Q.Differentiate between a variable and a constant in Java, giving one example of each declaration.
ChseodishaTextbookSubjectiveImportance★★★★★est
31% · 4/13 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Variable. A variable is a named box in memory that stores a value which may be changed any number of times while the program runs. It must be declared with a data type before use:
int marks = 85;
marks = 90; // allowed — a variable's value can change
Constant. A constant is a named value that must not change after it is first set. In Java it is created using the keyword final, and by convention its name is written in capitals:
final double PI = 3.14159;
// PI = 3.15; // NOT allowed — causes a compile-time error
Key differences:
| Point | Variable | Constant |
|---|---|---|
| Value | can change | fixed after being set |
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.