aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes q on instanceof operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "q on instanceof operator" Watch "q on instanceof operator" New topic
Author

q on instanceof operator

JayaSiji Gopal
Ranch Hand

Joined: Sep 27, 2004
Posts: 303
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?


SCJP 1.4, SCWCD 1.4<br /> <br />Thanks in advance!<br />Jayashree.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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.


Mike Gershman
SCJP 1.4, SCWCD in process
Pedro Ivo Dantas
Greenhorn

Joined: Sep 03, 2004
Posts: 10
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

Joined: Mar 13, 2004
Posts: 1272
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

Joined: Sep 03, 2004
Posts: 10
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

Joined: Mar 13, 2004
Posts: 1272
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

Joined: Mar 13, 2004
Posts: 1272
Sorry about all the edits, the above post is OK now.
JayaSiji Gopal
Ranch Hand

Joined: Sep 27, 2004
Posts: 303
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

Joined: Oct 23, 2004
Posts: 151
"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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: q on instanceof operator