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

Understanding instanceof operator

 
Ethan Bosco
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From K & B book pg : 159 instanceof operator

Given

interface Face{}
class Bar implements Face{}
class Foo extends Bar{}

there is a table which says
----------------------------------------------------------------------
First Operandinstanceof OPerandResult
-----------------------------------------------------------------------
Foo[]Foo, Bar, Facefalse
Foo[]Objecttrue
Foo[1]Foo, Bar, Face, Objecttrue

can someone give me some line of code that will validate the above table?

Thanks in advance
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use something similar to the code in this thread.
 
Ethan Bosco
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Barry,
I am still unable to understand maybe I should format the table better

my Second attempt, My q esp how Foo[] instance of Foo equals false any code that I can try
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ethan Bosco
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the code Barry , how about the third row in the table



Thanks again
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now we are in the correct forum:

 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies, I did not see you were actually trying out the statement, I thought you just wanted me to write the next four lines for you.

The above code compiles and runs for me (java 1.5.0).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic