• 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

Instance Doubt

 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why foo_array isn't instanceof Foo class ? thanks.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what the compiler is saying at line number 07? Foo[] and Foo are descendants in hierarchy
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:what the compiler is saying at line number 07? Foo[] and Foo are descendants in hierarchy



Theoretically i think if an object IS-A instanceof class type then why array of objects can't be.It is other thing that inconvertible type error here.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Foo[] is a container *which holds* Foo objects. think in this way : you put mangos in a bag, but mango is not a bag and vice-versa.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:Foo[] is a container *which holds* Foo objects. think in this way : you put mangos in a bag, but mango is not a bag and vice-versa.



Yes ,but when you will open this bag, you'll definitely find mangoes in it not bananas, so i think it might be possible here same as autoboxing like stuff.
 
Ranch Hand
Posts: 30
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:

Seetharaman Venkatasamy wrote:Foo[] is a container *which holds* Foo objects. think in this way : you put mangos in a bag, but mango is not a bag and vice-versa.



Yes ,but when you will open this bag, you'll definitely find mangoes in it not bananas, so i think it might be possible here same as autoboxing like stuff.



Mmm, na.
Mango bag(Foo array) is a BAG(array) but Mango(Foo) is Mango(still Foo).
Therefore, a Bag cannot be an instance of a Mango.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:Yes ,but when you will open this bag, you'll definitely find mangoes in it not bananas


Similarly, when you access an element of the array, it would satisfy IS-A foo condition. But as long as you try to test bag instanceof mango, it will always return false.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S.R Paul wrote:

saloni jhanwar wrote:

Seetharaman Venkatasamy wrote:Foo[] is a container *which holds* Foo objects. think in this way : you put mangos in a bag, but mango is not a bag and vice-versa.



Yes ,but when you will open this bag, you'll definitely find mangoes in it not bananas, so i think it might be possible here same as autoboxing like stuff.



Mmm, na.
Mango bag(Foo array) is a BAG(array) but Mango(Foo) is Mango(still Foo).
Therefore, a Bag cannot be an instance of a Mango.



I know what is a mango and bag ,no cheap jokes here.So keep silence.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anayonkar Shivalkar wrote:

saloni jhanwar wrote:Yes ,but when you will open this bag, you'll definitely find mangoes in it not bananas


Similarly, when you access an element of the array, it would satisfy IS-A foo condition. But as long as you try to test bag instanceof mango, it will always return false.



As i already said i know bag is not instanceof mango directly but it holds mangoes indirectly not bananas, when instanceof work for standalone object test then theoretically it will not wrong to test array of class objects that's it .It is other thing that there is no such automatic facility to open bag and compare each object and then again close bag like autoboxing type stuff.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:Please QuoteYourSources.
Henry


Book-> K&B6->Page-> 298->Chapter4-> TABLE 4-1
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:It is other thing that there is no such automatic facility to open bag and compare each object and then again close bag like autoboxing type stuff.



Ah... now I got what you were saying. Yes, there's no such facility. But I guess there's a reason for it - consider an array of Object - which contains a String, an Integer, a Cat, a Dog and so on. Even if there is such autoboxing facility, how would dog instanceof array would work? I mean should it perform instanceof test with first member, second member, or any other? And on what basis?

Its just my thought though...
 
S.R Paul
Ranch Hand
Posts: 30
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I know what is a mango and bag ,no cheap jokes here.So keep silence.


I ain't Joking and keeping shut wont solve problems. I was trying to say a basic rule in a simple way.
If the way I'm saying hurts you, I feel sorry.

Answer to the first question.
Now what matters is, instanceof operator can return true, if you compare mutually compatible types.
That means, the same type or the super types(it includes interfaces also)
Therefore, Foo[] can result in True if you check it against the 'Object'
(You would be end up with inconvertible types error if you compile you code)

Answer to your 'autoboxing' thought.
As 'Anayonkar Shivalkar' said most of the times you cannot assure that during Runtime you will process only the desired type of the array
The reason is as you think array is not that much type safe as it seems.
That's why we use casting in-order to make sure, that we are processing the correct type.
So, there is a risk that your 'autoboxing' may give ClasscastException during Runtime.
(Cz, you are not the only one who is going to use / maintain this code)

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anayonkar Shivalkar wrote:

saloni jhanwar wrote:It is other thing that there is no such automatic facility to open bag and compare each object and then again close bag like autoboxing type stuff.



Ah... now I got what you were saying. Yes, there's no such facility. But I guess there's a reason for it - consider an array of Object - which contains a String, an Integer, a Cat, a Dog and so on. Even if there is such autoboxing facility, how would dog instanceof array would work? I mean should it perform instanceof test with first member, second member, or any other? And on what basis?

Its just my thought though...



So where is the problem, we use instanceof to check whether true or false and it can be true or false.There is no monopoly that bag will have only mangoes, it can have bananas too.So instanceof will say false for bananas that's it.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:There is no monopoly that bag will have only mangoes, it can have bananas too.


no. if it is a (Object[])Object bag then it can hold mangos , banana etc. but If it is a Foo[](Mango bag) , then only Mangos are allowed otherwise you will end up with ArrayStoreException
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably you should be clear now. If the compiler cannot sense any way to cast between the comparison operands in instanceof, then it raises a compilation error. Clearly as others said you cannot cast from an Array holder to a single instance object.

Just a sample program to understand other such similar cases.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:

saloni jhanwar wrote:There is no monopoly that bag will have only mangoes, it can have bananas too.


no. if it is a (Object[])Object bag then it can hold mangos , banana etc. but If it is a Foo[](Mango bag) , then only Mangos are allowed otherwise you will end up with ArrayStoreException



Yes I know mango bag will allow only mangoes so any autoboxing type feature can open this bag and can check each mango using instanceof and again close it that was my idea that's it.
 
John Jai
Rancher
Posts: 1776
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please Be Nice guys. Thanks!
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:
Yes I know mango bag will allow only mangoes so any autoboxing type feature can open this bag and can check each mango using instanceof and again close it that was my idea that's it.



how about this?
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Folks, folks, please... let us focus (only)on technical issues
And as they say in art, criticize the art(i.e. creation), not the artist
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S.R Paul wrote:into an array, if inheritance involved



Inheritance involved means ? if we are certain then what is need to use instanceof to check that it is a instance or not of particular type ?? .We don't know that's why we are checking using intanceof .
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've cleaned up some posts from this thread which were taking this thread off track. Like others have said in this thread, feel free to continue the discussion but at the same time please Be Nice. Any posts violating that will be deleted.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This topic has been over so no more post reply here.thanks
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:

Anayonkar Shivalkar wrote:

saloni jhanwar wrote:Yes ,but when you will open this bag, you'll definitely find mangoes in it not bananas


Similarly, when you access an element of the array, it would satisfy IS-A foo condition. But as long as you try to test bag instanceof mango, it will always return false.



As i already said i know bag is not instanceof mango directly but it holds mangoes indirectly not bananas, when instanceof work for standalone object test then theoretically it will not wrong to test array of class objects that's it .It is other thing that there is no such automatic facility to open bag and compare each object and then again close bag like autoboxing type stuff.



Not to beat a dead horse, but I can't imagine how a mechanism can be done. Unlike wrapper objects, which wrap a single value, array objects (and collection objects) can hold more than one element. There isn't a way to manually box and/or unbox that will always work, hence, I can't imagine how Java can do it in an "auto"mated way.

Henry
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

saloni jhanwar wrote:

Anayonkar Shivalkar wrote:

saloni jhanwar wrote:Yes ,but when you will open this bag, you'll definitely find mangoes in it not bananas


Similarly, when you access an element of the array, it would satisfy IS-A foo condition. But as long as you try to test bag instanceof mango, it will always return false.



As i already said i know bag is not instanceof mango directly but it holds mangoes indirectly not bananas, when instanceof work for standalone object test then theoretically it will not wrong to test array of class objects that's it .It is other thing that there is no such automatic facility to open bag and compare each object and then again close bag like autoboxing type stuff.



Not to beat a dead horse, but I can't imagine how a mechanism can be done. Unlike wrapper objects, which wrap a single value, array objects (and collection objects) can hold more than one element. There isn't a way to manually box and/or unbox that will always work, hence, I can't imagine how Java can do it in an "auto"mated way.

Henry



I believe one day that java version will come to prove me
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:

Henry Wong wrote:

Not to beat a dead horse, but I can't imagine how a mechanism can be done. Unlike wrapper objects, which wrap a single value, array objects (and collection objects) can hold more than one element. There isn't a way to manually box and/or unbox that will always work, hence, I can't imagine how Java can do it in an "auto"mated way.

Henry



I believe one day that java version will come to prove me


If there is no real need for the feature, it will would never be considered to be added to the language.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohamed Sanaulla wrote:

saloni jhanwar wrote:

Henry Wong wrote:

Not to beat a dead horse, but I can't imagine how a mechanism can be done. Unlike wrapper objects, which wrap a single value, array objects (and collection objects) can hold more than one element. There isn't a way to manually box and/or unbox that will always work, hence, I can't imagine how Java can do it in an "auto"mated way.

Henry



I believe one day that java version will come to prove me


If there is no real need for the feature, it will would never be considered to be added to the language.



Yes, Necessity is the mother of invention.
reply
    Bookmark Topic Watch Topic
  • New Topic