• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

instanceOf explanation needed

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pls explain the following:

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

instance instanceOf what result my comments
Foo[] Foo, Bar, Face, Object false OK
Foo[1] Foo, Bar, Face, Object true How??
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir please write question in more detail as we does not understand that what are you trying to say exactly , i am not hurting you but please explain your question again.

Thanks
Regards
Gaurav
 
Suhas Wadadekar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pls explain the following: This is from K & B book Chapter 3

interface Face(){}
class Bar implements Face{}
class Foo extends Bar{} //code ends here


Case 1:
instance: Foo[]
Foo[] instanceOf Foo(or Bar or Face or Object)
result: false
my comments: OK

Case 2:
instance: Foo[1]
Foo[1] instanceOf Foo(or Bar or Face or Object)
result: true
my comments: How? What difference does the number 1 make?
 
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Suhas Wadadekar:
Pls explain the following: This is from K & B book Chapter 3

interface Face(){}
class Bar implements Face{}
class Foo extends Bar{} //code ends here


Case 1:
instance: Foo[]
Foo[] instanceOf Foo(or Bar or Face or Object)
result: false
my comments: OK

Case 2:
instance: Foo[1]
Foo[1] instanceOf Foo(or Bar or Face or Object)
result: true
my comments: How? What difference does the number 1 make?





Foo[] is of type Array. It is an array of Foo Objects. The array of Foo Objects is not an instance of Foo/Bar/Face etc. So in the first case it prints false.

Foo[1] means the value inside the Foo array. Since this can hold only Objects of Foo type, Foo[1] will give an Object of Foo type. And Foo is an instance of Foo/Bar/Face etc. So this results in true.

Hope this helps
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i have facing same problem but u wrote that u understood case 1,plz can u explain case 1 by using example.
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok
what i understood by this line "Foo[] is of type Array. It is an array of Foo Objects. The array of Foo Objects is not an instance of Foo/Bar/Face etc. So in the first case it prints false."



Foo[] f=new Foo[]{new Foo()};
System.out.println(f instanceof Foo);//but this is compile time error
 
Suhas Wadadekar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Rajah....that did explain the things....i don't know why...but the 3 rd chapter was a disaster for me.....

and gowher..... rajah provided the explanation to ur doubt too...do u still need any more explanation?

1. a Foo instance will pass the instanceOf test of Foo, Bar, Face, Object....
2. but Foo[] is an array type...it refers to an array and not to any instance of Foo, Bar, Face, Obeject
3. Hence Foo[] fails the instanceOf test

While in second case Foo[1] represents a Foo object
(just like: int[] array = new int[2]; where array[0] refers to an int value stored in the arry)
Hence it passes the instanceOf test....
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought, I would bring to your notice a minor error in the above discussion.

There is no instanceOf operator in Java. Its instanceof.

Many a times, students overlook this fact.
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Suhas
Does Foo[] fails the instanceOf test means this

Foo[] f=new Foo[]{new Foo()};
System.out.println(f instanceof Foo);//compile time error
Is i am Right?
 
Suhas Wadadekar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes...the code does not compile because they are inconvertible types

Thanks Aum Tao for pointing out my negligence about the keyword instanceof
[ July 11, 2006: Message edited by: Suhas Wadadekar ]
 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup as stated 'System.out.println(f instanceof Foo);' would print false for the above stated reasons. but 'System.out.println(f[0] instanceof Foo);' will compile and result in true.
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yup as stated 'System.out.println(f instanceof Foo);' would print false for the above stated reasons.



It will result in a compile time error.
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic