Consider the following code snippet:
1 2 3 |
if (i == 15.0) { System.out.println("true"); } |
Which one of the following declarations of the variable i
will compile without errors and print true
when the program runs?
- int i = 017;
- int i = 15.0f;
- int i = 15L;
- int i = 15.0;
I will comment out the solution in the next few hours. Stay tunned.
The solution is the first one, int i = 017;
Can you tell me why?
Putting a ‘0’ before a number makes that number an octal number. A decimal equivalent of 017 (in octal) is 15. If you attempt an implicit conversion from float, long, or double types (as given in options 2, 3, and 4 respectively) to an integer, you will a get compiler error.