Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Test Question - Explanation?

 
Dave Bosky
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
------------
Thanks,
Dave
 
Dmitry Melnik
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#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
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Given:

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.
hth
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic