• 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

doubt in instanceof operator..

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From K&B book..scjp

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

Now..my doubt is:

Foo[] instanceof Foo
Foo[] instanceof Bar
Foo[] instanceof Face

"the result in K&B book is that all return "false", but i did not understand how the result is "false",
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the problem is that when an array of object foo is created each object by default is null. so whenu use the instanceof operator each returns false if the objects within the array are not assigned any references of type Foo or Bar or Face.
 
Shahid Ahmed
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks..
i understood.,but what about this:

Foo[] instanceof Object
the answer is "true" , how come??
 
sri chandan kalavapudi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you created an array object of type Foo and any object in java passes the instanceof test when checked with Object class.
 
Shahid Ahmed
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, when we say: Foo[] instanceof Object , here the default value for Foo[] will be "null" ? if it is null,it means that null instanceof Object always returns true. ?








Very much Thanks for clearing my doubt..
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Let me clear your doubt:

first of all :
instanceof Operator should be used if operand on both side are compatible.
compatible means they are under same hierarchy structure.


Now understand the meaning of this :

f --> refer to one array object in the heap, and element of that array can refers to Foo or it subtype.

so if you try:
f instance Foo ---> this will not compile because f refer to an array object, Foo doesn't come under hirerachy of array.

f instanceof Object --> this will compile and will give you true, because Object is superclass of array Object so under same hirerachy.

f[0] instanceof Foo ---> This will compile and will return you false,
f[i] --> can refer to Foo but currently it is null, NULL belong to every one , but doesn't refer to any one. so null instanceof anything will return you false

f[0] isntanceof Object --> this will compile and will return you false. null belong of Object also.

f[0] = new Foo();

f[0] instanceof Foo ---> This will compile and will return you true

f[0] instanceof Object ---> This will compile and will return you true, because Object is also superclass of Foo.

Hope this will clear you.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the following lines are printed,different outputs are obtained.


The output is true.


The output is false.

Can anyone please explain.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dean Jones:
When the following lines are printed,different outputs are obtained.


The output is true.


The output is false.

Can anyone please explain.



Hi Dean,

The code referred to by "line 1" returns true since an empty String to the left of "+" operator caused it to be overloaded and concatanates with null making it a String. As all objects in Java inherit from java.lang.Object the instanceof will return true.

The "line 2" however directly tests if a null reference derives from the Object or is a subclass of it however sice it does not refer to any object it by no means can be any of them. That's why the instanceof returns false.
 
Dean Jones
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul it helped.
 
A timing clock, fuse wire, high explosives and a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic