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:
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?
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?