File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply locked New topic

instanceof operator

cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
Dear all,
I apologize for posting a topic that should have been certainly already discussed.
I've searched in the archives but didn't find exactly what I was looking for.
From Dan's mock exam:

Answer: compiler error
The expression used to set the value of variable b3, "r1 instanceof Blue" causes a compiler error because the type of variable r1, Red, can not be converted to type Blue

So as far as I understand, we may use instanceof operator between classes assumed that one of these is a subclass of the other one(the first operand may be a subclass of the second, or vice-versa).
This avoid compiler error, if not runtime error.
Can we say that the rules governing compiler errors for instanceof Operator are exactly the same as for object reference assignement except for that case and except that we can't specify an interface as first operand?
By this I mean:
class A {}
class B{};
A a = new A();
B b = new B();
a = b; // object reference assignement
b instanceof a // instanceof operator
In the first case, compiler checks if a is a superclass of b.
In the second case, compiler only checks if a is a superclass or a subclass of b
But in the other cases, the same rules apply in both cases.
Example: we can't say for an array element to be a instanceof any class except the Object one, exactlty as for object reference assignement:

although this code is correct and compiles well

Thanks in advance for your answer,
NB: I thought the arrays were an exception to the rule telling that a local variable must be initialized before to be used.
In the above example, it's apparently not the case, because if I replace
in the main method by just:

An exception is thrown: variable i may not have been initialised...
What's the correct interpretation?

SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
Dave Johnson
Ranch Hand

Joined: May 25, 2003
Posts: 111
Hi Cyril, the first problem is that an instance of Red r1 has no relationship with Blue, they both extend Color but nothing more. So the line boolean b3 = r1 instanceof Blue; would cause a compiler error.
Secondly the question about arrays. The line int [] i = new int[5]; this is only initialising the size of the array the contents are automatically intialised as expected. Don't get confused between the size of the array and its contents.
Hope this helps, Dave.
Dave Johnson
Ranch Hand

Joined: May 25, 2003
Posts: 111
Just noticed this thread has been posted twice, hope this was an accident.
cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
Thanks all for your explanations.
Yes, it is an accident (The first time, I thought the message wasn't sent, because the server told me I had to send it again a few minutes later...)
I'm sorry for what happened,
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Closing the dup
You can continue this discussion at

[Blog] [Blogroll] [My Reviews] My Linked In
I agree. Here's the link:
subject: instanceof operator
It's not a secret anymore!