Bookmark Topic Watch Topic
  • New Topic

instanceof operator

 
cyril vidal
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Explanations:
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,
Cyril.
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?
 
Dave Johnson
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
  • Report post to moderator
Just noticed this thread has been posted twice, hope this was an accident.
 
cyril vidal
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Report post to moderator
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,
Cyril.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Report post to moderator
Closing the dup
You can continue this discussion at http://www.coderanch.com/t/242427/java-programmer-SCJP/certification/instanceof-operator
 
    Bookmark Topic Watch Topic
  • New Topic