Wednesday, November 3, 2010
What will happen when we try to compile the following code?
a. The compiler objects to line 3 comparing an Object with an array.
b. The compiler objects to line 5 casting an Object to an array of int primitives.
c. The compiler objects to line 11 comparing an Object to an array of Objects.
d. It compiles without error.
Solution : Choice D is the correct answer.
The "instanceof" operator tests the class of an object at runtime. It returns true if the type of the left-hand argument is the same as, or is some subtype of, the class or interface specified by the right-hand operand. Arrays are also objects, so they can be referred to by variables of type Object. Hence comparing Object type to array using instanceof is allowed. So choices A, B and C are incorrect.
Source : "Facebook community"