This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof question" Watch "instanceof question" New topic
Author

instanceof question

M. Herder
Greenhorn

Joined: Feb 24, 2003
Posts: 5
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

Joined: Sep 27, 2002
Posts: 51
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.


SJCP 1.2
Rajinder Yadav
Greenhorn

Joined: May 13, 2003
Posts: 27
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


When faced with an easy thing to do and a hard thing to do, always pick the right thing to do!<p><a href="http://yadav.shorturl.com" target="_blank" rel="nofollow">Rajinder Yadav</a>
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
In the original code pasted by M. Herder, why compiler is not assigning false to test ?
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787

Hi M. Herder,
Try above code. It assigns the false to test.
[ May 20, 2003: Message edited by: Barkat Mardhani ]
M. Herder
Greenhorn

Joined: Feb 24, 2003
Posts: 5
OK - I see the difference!
Thanks a lot!
 
Don't get me started about those stupid light bulbs.
 
subject: instanceof question