Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instanceof

 
Jini Varghese
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 type.
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.
I think 3 is the answer. Is there any other possibility ?.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think answer 3 is wrong. Take a look at the following code and try running it

The correct answer is 4.
Ajith
 
Sivaram Ghorakavi
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I too agree it's 4 that is CLOSE. But the question is slightly confusing.. The RHS of the instanceof operator is always validates the Class name up in the hierarchy not the instance.
Currect me if wrong.
 
Jini Varghese
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys..
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right Sivaram, but you can arrive at the answer using method of elimination. Infact the answer 4 depicts a typical usage of the instance operator.
If ref1 and ref2 are instances of the same class, you can use the instanceof operator to infer the type of the class these objects belong to. However, in situations where you have a multiple inheritance relationship, you will have to be little care because you will get similar results if one of the two objects is actually and instance of a superclass.
Sorry for digression, but I'd prefer to use the getClass() on the references and compare the Class objects returned instead of using the instanceof operator. The Class object approach, IMHO is far more accurate than the instanceof approach.
Hope that helps,
Ajith
 
Rajpal Kandhari
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jini,
Explanation given by Ajith is very good. And I would like to tell you in General about instanceof operator (hoping it will help you in clearing you're funda), I have taken these lines from RH:
"The instanceof operator tests the class of an object at runtime. The left-hand argument can be any object reference expression, usually a variable or an array element, while the right hand operand must be a class, interface, or array type. You cannot use a java.lang.Class object or its string name as the right-hand operand.
The instanceof operator return true is the class of the left-hand argument is the same as, or is some subclass of, the class specified by the right-hand operand.
The right hand operand may equally well be an interface. In such a case, the test determines if the object at the left-hand argument implements the specified interface.
You can also use the instanceof operator to test if a reference refers to an array. Since arrays are themselves objects in Java, this is natural enough, but the test that is performed actually checks two things: First it will check if the object is an array and then it checks if the element type of that array is some subclass of the element type of the right-hand argument."
As mentioned by Ajith, using of getClass() method of Class class to find if the object belongs to the class. It can be done to find out if an object is in fact an array, without regard to the base type. You can do this using the isArray() method of the Class class. For ex
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic