What will the following code snippet print?
Object t = new Integer (107); int k = (Integer) t.intValue ()/9; System.out.println (k);
Select 1 option
Correct Option is : C
Compiler will complain that the method intValue()
is not available in Object.
This is because the . operator has more precedence than the cast operator.
So you have to write it like this:
int k = ((Integer) t).intValue ()/9;
Since both the operands of / are ints, it is an integer division.
The resulting value is truncated (and not rounded).
The above statement will print 11 and not 12.