Forums Register Login

instanceof operator

+Pie Number of slices to send: Send
Hi,
This question is from the third chapter self test section in Kathy & Bates book.
1.import java.awt.*;
2.class Ticker extends Component {
3.public static void main(String args[]){
4.Ticker t=new Ticker();
5.
6. }
7.}
which two of the following statements,inserted independently,could be legally inserted into line 5 of this code?(Choose two).

There were 6 options given A-F.The answers given were B and D which were as follows
B. boolean test=(t instanceof Ticker);
D. boolean test=(t instanceof Component);
But the following option
F. boolean test=(t instanceof String); /*was given as incorrect bcos String
class was not in the hierarchy of the t object.*/
I think the above is legal and is same as saying boolean test=false.
Is my assumption right?If not Why?
Thanks in advance,
Atul Chandran.
+Pie Number of slices to send: Send
Can I suggest that you compile the example with that option at line 5? Then you will see why it cannot go there. Sometimes "thinking" is not the best thing to do. Look at the result the compiler gives and then try to reason why it behaves as it does.
[ October 28, 2004: Message edited by: Barry Gaunt ]
+Pie Number of slices to send: Send
 

Can I suggest that you compile the example with that option at line 5? Then you will see why it cannot go there. Sometimes "thinking" is not the best thing to do. Look at the result the compiler gives and then try to reason why it behaves as it does.


OK, I tried compiling and got "inconvertible types", which is just what
instanceof is supposed to test for and return false.

What is wrong with "boolean test=(t instanceof String);"? t is a reference variable and String is a class.
+Pie Number of slices to send: Send
If the compiler KNOWS that there is no possible way that the instanceof can EVER be true, from a type compatibility perspective, it will produce a compile error.

In this example, the compiler is absolutely certain that t instancef String can never succeed, so it's stopping you. Remember that Java is *always* trying to use type-safety to raise problems at compile-time rather than runtime, so the compiler looks beyond the fact that you're testing a reference variable, and compares the *declared type* of that reference variable with the right-hand operand in the instanceof expression, and if it discovers a "NO FRICKIN' WAY" scenario, it complains

There ARE a lot of scenarios in which the compiler cannot be certain until runtime, but this example is not one of those...

So, your logic makes sense in that, "Shouldn't it just do the test and return false?" But in this case, the compiler knows it'll never work, and is telling you that something is flawed with the code itself for even THINKING that it might work at runtime.

cheers,
Kathy
+Pie Number of slices to send: Send
Kathy sez:

So, your logic makes sense in that, "Shouldn't it just do the test and return false?" But in this case, the compiler knows it'll never work, and is telling you that something is flawed with the code itself for even THINKING that it might work at runtime.



But this would compile on line 5:
And a modern optimizing compiler would compile "5 == 8" as false
+Pie Number of slices to send: Send
Go through the following example from Dan's exam.You will understand instanceOf operator better! . This explanation has helped me to questions that include instanceOf operator correctly.Hope this is going to be helpfull.

class Color {}
class Red extends Color {}
class Blue extends Color {}
class A {
public static void main (String[] args) {
Color color1 = new Red(); Red color2 = new Red();
boolean b1 = color1 instanceof Color;
boolean b2 = color1 instanceof Blue;
boolean b3 = color2 instanceof Blue;
System.out.print(b1+","+b2+","+b3);
}}

Ans : Compiler error

Explanation

The type of the reference color2 is Red. Since Red is not a subclass or a superclass of Blue, the expression color2 instanceof Blue is rejected at compile-time. Please note: The expression, x instanceof T, produces a compile-time error whenever the cast expression (T)x produces a compile-time error. If the program had been able to compile and run, the expression color1 instanceof Color would evaluate to true at run-time. The reference color1 refers to an instance of type Red. Since Red is a subclass of Color, the expression color1 instanceof Color would evaluate to true at run-time. The expression, color1 instanceof Blue would evaluate to false at run-time. The reference, color1, is of type Color. Since Color is a superclass of Blue, the expression, color1 instanceof Blue, is accepted at compile-time. The type of the object instance referenced by color1 is Red. Since Red is not Blue or a subclass of Blue, the expression, color1 instanceof Blue, would be false at run-time.

Veena
+Pie Number of slices to send: Send
boolean test=null instanceof String;//false is assigned to test
Why the compiler does'nt generate an error at compile time?? It can be sure that a null can never be a String.
+Pie Number of slices to send: Send
In the case of null as the first operand, the compiler is conforming to the JLS which says:

The type of a RelationalExpression operand of the instanceof operator must be a reference type or the null type; otherwise, a compile-time error occurs. The ReferenceType mentioned after the instanceof operator must denote a reference type; otherwise, a compile-time error occurs.



The RelationalExpression referred to is the lefthand operand. This could be a reference variable referring to a Class object. Because the reference variable could have the value null, the JLS is instructing compiler writers to defer this case to run time.

As Kathy said in the case of the lefthand operator being a Type (a literal, if you like), the compiler has access to all the information it needs to make the decision to allow further compilation or not.

Don't forget that the Java language can be used to program pretty complex programs, even Java Compilers. So the JLS has to allow for some really strange goings on.
[ October 30, 2004: Message edited by: Barry Gaunt ]
Doody calls. I would really rather that it didn't. Comfort me wise and sterile tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 927 times.
Similar Threads
Question from Ch 3 Self Test, Sierra/Bates Book
instanceof Problem
Kathy/Bert book ch 3 self test q 2
instanceof
What is valid types to check against using Instanceof
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:40:13.