This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof operator" Watch "instanceof operator" New topic
Author

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


SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
Damien Howard
Ranch Hand

Joined: Apr 01, 2003
Posts: 456
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.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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 ]
cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceof operator
 
Similar Threads
instanceof
instanceof operator
instanceof operator
instanceof question
Doubt from Dan's test