Which of the following statements are true? 1) The instanceof operator can be used to determine if a reference is an instance of a class, but not an interface. 2) The instanceof operator can be used to determine if a reference is an instance of a particular primitive wrapper class 3) The instanceof operator will only determine if a reference is an instance of a class immediately above in the hierarchy but no further up the inheritance chain 4) The instanceof operator can be used to determine if one reference is of the same class as another reference thus Marcus says the correct answer is 2 But I think it should be 4 What do u think guys
Please refer RHE pg51 which says "the right hand operand must be a class, interface, or array type". This will make answer 2 correct. Choice 4 seems to be incomplete....
Number 2 is correct. Look that it says primitive "wrapper" Number 4 may be bad because I understand that it is comparing two references. ref1 instanceof ref2, and the syntax is: ref1 instanceof class1.
Hi , To see if two objects A and B are of the same class you can not use if(A instanceof B), instead use if (A.getClass() == B.getClass()) . Do refer to Exam Cram, 2nd. Ed., pg. 106. Regds.