-
Which of the following are legal lines of Java code?
1. int a = (int)888.8;
2. byte b = (byte)100L;
3. long c = (byte)100;
4. byte d = (byte)100L;
-
- 1 and 2
- 2 and 3
- 3 and 4
- All statements are correct.
- NA
Correct Option: D
All four statements are correct
Option 1. is correct because when a floating-point number (a double in this case) is cast to an int, it simply loses the digits after the decimal but doesn't throw any error.
Option 2 & option 4, are correct because a long can be cast into a byte. If the long is over 127 (byte max value), It loses its most significant (leftmost) bits.
Option 3. It works perfectly and casting is not needed, because a long can store a byte.