my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Test Question - Explanation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Test Question - Explanation?" Watch "Test Question - Explanation?" New topic

Test Question - Explanation?

Dave Bosky
Ranch Hand

Joined: Dec 16, 2003
Posts: 72
I was told the answer to the following question is #2 but I thought it would be answer #4. Could someone explain this to me? I thought the instanceof operator checks the type of object.

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
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
#4 is wrong. Expression (A instanceof B) will evaluate as true not only if A has the same type as B, but also if
A itself or one of it's superclasses implements interface B or;
A extends (directly or not) class B
For instance:

Both x1 and x2 are true
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
The instanceof operator returns true if the type of the object referenced by the left operand is assignment compatible with the type of the right operand. The test for assignment compatibility is less restrictive than a test for an exact match between two types. Although the instanceof operator will return true if the types of the left and right operands match exactly, instanceof will also return true if the type of the left operand is a subclass of the right operand. If you want to test for an exact match between the types of two objects, then use the equality operator, ==, to verify that both classes have the same Class object: obj1.getClass() == obj2.getClass().

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="" target="_blank" rel="nofollow">Try my mock exam.</a>
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
I find it helpful to note that the wording of #4 is referring to the right hand side (rhs) as a reference. The rhs must be an explicitly named Class that the left operator may be cast to, or an Interface.

Line 1 will compile, run and return true, an Integer may be cast to a Number
Line 2 will not compile, the rhs is a reference, not a Class or Interface name as required. (this is the situation answer #4 refers to)
Line 3 will not compile, you may not use instanceof with a dynamically generated rhs
Line 4 will compile and return false when run, Collection is an interface, whether or not i1 implements it will not be determined until runtime.
Line 5 will not compile, i1 is known at compile time to not be castable to type Thread.
subject: Test Question - Explanation?
Similar Threads
A Question from Marcus exam 3 about instanceof
instanceof Marcus Mock 3 No. 6
Using instanceof operator
instanceof operator