• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question from Ch 3 Self Test, Sierra/Bates Book

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just took the Self Test from Chapter 3 of Kathy Sierra & Bert Bates "Sun Certified Programmer & Developer for Java 2 Exam Study Guide" and I am slightly bewildered by question #2. Here's the question:
Given the following:
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 legally be inserted into line 5 of this code? (Choose two.)
A. boolean test = (Component instanceof t);
B. boolean test = (t instanceof Ticker);
C. boolean test = t.instanceof(Ticker);
D. boolean test = (t instanceof Component);
E. boolean test = t.instanceof(Object);
F. boolean test = (t instanceof String);
I know that A,C, and E are illegal statements (will not compile). But it looks to me like B,D, and F are all legal statements that can bee added to line 5 and not prevent sucessful compilation. But the question states that there are only two correct answers, and the answer key says they are B and E. Is this an inconsistency in the phrasing of the question and the expected answers? Or am I just missing something so obvious that I just can't see it? Any help is appreciated.
David.
[ April 18, 2003: Message edited by: David Willis ]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since it is impossible for t to ever be a String, the comparison is meaningless since the compiler knows that it must always be false. So it gives you a compile error.
 
David Willis
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I didn't realize that the Java compiler checked to see if the variable operand of instanceof was in the hierarchy tree of the type operand. I thought it just confirmed syntax, and performed the analysis at runtime. Does the Java Language Specification demand that this compilation time analysis of the hierarchy tree take place, or is it a unique characteristic of Sun's javac?
[ April 16, 2003: Message edited by: David Willis ]
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See JLS 15.20.2
Type Comparison Operator instanceof
If a cast of the RelationalExpression to the ReferenceType would be rejected as a compile-time error, then the instanceof relational expression likewise produces a compile-time error. In such a situation, the result of the instanceof expression could never be true.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
instanceof is not a method into which you can pass an argument, so E is wrong.
B and D are therefore right.
Incidentally, they would both evaluate to true because this is what you get when the right operand is the same class or superclass of the left operand.
 
David Willis
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Thomas. I found a link the the JLS in a earlier thread you posted in, and have bookmarked it for future reference.
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yikes! Howdy David, I just wanted to confirm something -- you said that the book says "B and E" are correct?
Because in our copies of the book, it says "B and D" (which are indeed the correct answers), for the reasons indicated by others. (In a nutshell, if the compiler can tell, for certain, that this instanceof could NEVER work at runtime, you'll get a compiler error).
So now I'm all worried about where the "B and E" answers came from. I'm hoping that you simply copied it down wrong
Please let me know, or I won't get any sleep tonight
-Kathy
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what I would like to add here is that the instanceof comparison works similar to the == for objects, that is the object instance being compared have to be of similar types , classes, subtypes in the hierachy to match...

However when checking if a class object is an instanceof a unrelated interface, the class need not implement the interface... there will be no compile time error but a runtime error... Am I right in this assumption?
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You won't get a runtime error, just false being returned.
Remember that x instanceof y always yields true if the class (or interface) y can be found in the class hierarchy of x, that is:
y is a superclass of x or x itself
y is an interface implemented by any superclass of x or by x itself

Here is some code which indicates what happens with interfaces.
 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,
I fully agree with 'Kathy'. B and D are correct answers, can you explain how E will the answer.
 
David Willis
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No worries. I screwed up when composing my post. The book does say B and D are the correct answers (and that's what I have written in my self test notebook, too :-) .) Sorry about causing any unwarrented panic.
reply
    Bookmark Topic Watch Topic
  • New Topic