Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

q on instanceof operator

 
JayaSiji Gopal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the following answers can be legally placed in the place of XXX?
class Check {
Check() { }
}

public class ICheck extends Check {
public static void main ( String[] args) {
Object o = new ICheck();
Check i = new ICheck();
Check c = new Check();

if ( o instanceof XXX) {
System.out.println("True");
}
}
}
A) Object, ICheck only
B) Check , ICheck only
C) Object only
D) Object, Check, ICheck

i answered c, but the correct answer is d. yy?
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To see whether "aaa instanceof B" is legal, consider the type of aaa. If aaa could possible hold an object that can be cast to type B, then it's legal.

In this case, the class Object variable o is a reference to any object, so it could hold an object of any class you put on the right side of instanceof.
 
Pedro Ivo Dantas
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hum, I think you are not right Mike. You have to consider the hierarchy of the classes of the object being compared. Variable o is declared as Object, but it holds a reference to a ICheck instance. ICheck extends Check, which extends Object, just like any other java class. Therefore it is a instance of any of these classes, and will passa the instanceof comparation for any of these.
If you compare any object with the class Object (xxx instanceof Object), the comparation will be true, because every object extends Object. But remember, the object in the left MUST BE of the same class or a subclass of the class on the right.
Hope I've made myself clear.
[]s
Pedro Ivo
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tested the three possibilities,
o instanceof Object
o instanceof ICheck
o instanceof Check

they all compile OK.

The question didn't ask if the conditional was true, it asked if it was legal, meaning it compiles and excutes without error.
In this case, the three possibilities are also true, since o is an instance of class ICheck and ICheck is a subclass of both Check and Object.
 
Pedro Ivo Dantas
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Weel, just to make this topic clear: when using the instanceof operator, the left operand can be any java object, while the right hand can be any java class. It will return true if the object is a instance of the class, or any of it subclasses. This relation is also valid for interfaces.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know I sound like a language lawyer, but the left side of instanceof is an object reference, not an object. To avoid a compilation error, there must be some class whose instances could both be referenced by the left operand and be cast to the right operand class.

In other words, "a instanceof B" is legal if there is some possible code that would make that expression true. If that is impossible, Java warns you at the compilation stage.

Consider this code:


In "b instanceof A", I could assign a B object to b to make the expression true.

In "a instanceof B", I could assign a B object to a to make the expression true.

In "b instanceof C", there is nothing I could legally assign to b to make the expression true, so the Java compiler warns me that it thinks I made an error.
[ November 19, 2004: Message edited by: Mike Gershman ]
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about all the edits, the above post is OK now.
 
JayaSiji Gopal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike,

If u have two classes,
Class A {}
Class B extends A {}

b instanceof A -> returns true.
a instanceof B -> returns false, right?
 
Joe Borderi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"a instanceof B -> returns false, right?"

In your example, You don't have enough information to state the result. The result depends on how a is defined:

A a = new A(); //then if(a instanceof B) --> returns false
A a = new B(); //then if(a instanceof B) --> returns true
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic