Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instanceof question

 
M. Herder
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I hava a question concerning SELF TEST question #2 in K&B chapter 3 (great book - worth every cent).

I don�t understand why the 'instanceof' line gives me a compiler error. Why it does not resolve the expression to 'false'?
Thanks a lot!
[ May 20, 2003: Message edited by: M. Herder ]
 
peter greaves
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because the compiler knows that an instance of a Ticker cannot possibly be a string - it can see that String extends Object and Ticker extends Component which itself extends Object. so a Ticker can not be an instance of String.
 
Rajinder Yadav
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the instanceof operator works up a class hierarchy, not acorss
peter is right, the compiler is able to see this and thus gives you an error

class a and b are both instances of class base
class a is not an instance of class b and vice versa
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the original code pasted by M. Herder, why compiler is not assigning false to test ?
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi M. Herder,
Try above code. It assigns the false to test.
[ May 20, 2003: Message edited by: Barkat Mardhani ]
 
M. Herder
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK - I see the difference!
Thanks a lot!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic