Forums Register Login

instanceof operator

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
I'm not sure about the instanceof question, but as far as the array goes.
You must always initialize a local variable before use, the compilor does not do it for you.
What they meant when discussing arrays was that the elements of the array are pre-initialized for you regardless of the array's location. The array itself however is not.
+Pie Number of slices to send: Send
 

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?


No. If a cast causes a compiler error, then instanceof causes a compiler error.
class C {}
interface I {}
(C)x, (I)x, (String[])x
x instanceof C, x instanceof Y, x instanceof String[]

I thought the arrays were an exception to the rule telling that a local variable must be initialized before to be used.


All local variables must be initialized before use. The elements of an array are automatically initialized.
int[] i; // must be initialized
int[] j = int[10]; // j[0] is initialized.
+Pie Number of slices to send: Send
I think about assignment, cast and instanceof in terms of type conversions � widening conversions and narrowing conversion.
x instance of Y
At compile-time, the conversion from the *declared* type of x to Y must be either a *widening* or a *narrowing* reference conversion.
At run-time if the conversion from the *actual* type of x to Y is a *widening* conversion, instanceof returns true.
Widening: subclass to a class
Narrowing: class to a subclass
Widening: class to Object, interface to Object, array type to Object
Narrowing: Object to class, Object to interface, Object to array type
The complete list of reference type conversions is in JLS 1.5.4 and 1.5.5.
[ July 12, 2003: Message edited by: Marlene Miller ]
+Pie Number of slices to send: Send
OK. Thanks Marlene for your very useful explanations.
I was particulary wrong in thinking that an interface could'nt be a left hand operand of instanceof operator (because we can't instantiate an interface).
But here's an example how this can work:

output: true
Now, if I change the code to the following ( J doesn't more extend I):

output: compile well and output : false;
now I change again the code, so that class A doesn't more implement J:

Compiles well (cast avoids error compiling) but no output: java.lang.ClassCastException . Indeed, a doesn't implement J.
I think this little example helped me to understand more and will perhaps help other ones...
Cyril.
Whose rules are you playing by? This tiny ad doesn't respect those rules:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 913 times.
Similar Threads
instanceof operator
instanceof operator
Doubt from Dan's test
instanceof
instanceof question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:07:25.