Computer Applications
Which of the following is a valid Java declaration?
Values & Data Types Java
3 Likes
Answer
int num = 'B';
Reason — Let's analyze each of the options:
1. float x = 12.5; — Incorrect
- A floating-point literal (like
12.5) is by defaultdoublein Java. - To store it in a
floatvariable, explicitly specifyforF:float x = 12.5f; // Corrected version
2. char ch = "A"; — Incorrect
- A
charmust be enclosed in single quotes (''), not double quotes (""). - Correct version:
char ch = 'A';
3. boolean b = 0; — Incorrect
- Java does not allow assigning integers (
0or1) toboolean. - Only
trueorfalseis allowed. - Correct version:
boolean b = false;
4. int num = 'B'; — Correct
- A
charin Java can be assigned to anintbecause characters are internally stored as Unicode values (ASCII-compatible). 'B'corresponds to Unicode 66, sonumwill store 66.
Answered By
2 Likes