File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof operator" Watch "instanceof operator" New topic
Author

instanceof operator

Atul Chandran
Greenhorn

Joined: Oct 24, 2004
Posts: 22
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.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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.


Mike Gershman
SCJP 1.4, SCWCD in process
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
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
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
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


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Atul Chandran
Greenhorn

Joined: Oct 24, 2004
Posts: 22
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.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceof operator