aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Instance Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Instance Doubt" Watch "Instance Doubt" New topic
Author

Instance Doubt

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Why foo_array isn't instanceof Foo class ? thanks.


Tell the difficulties that i am difficult.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

what the compiler is saying at line number 07? Foo[] and Foo are descendants in hierarchy
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Feb 09, 2012
Posts: 583

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.
S.R Paul
Ranch Hand

Joined: Mar 22, 2012
Posts: 30

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.

=-- Anything less than immortality is a complete waste of time --=
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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

Joined: Feb 09, 2012
Posts: 583

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

Joined: Feb 09, 2012
Posts: 583

Henry Wong wrote:Please QuoteYourSources.
Henry

Book-> K&B6->Page-> 298->Chapter4-> TABLE 4-1
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

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

Joined: Mar 22, 2012
Posts: 30

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

Joined: Feb 09, 2012
Posts: 583

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

Joined: Jan 28, 2008
Posts: 5575

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
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Feb 09, 2012
Posts: 583

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
Bartender

Joined: May 31, 2011
Posts: 1776
Please Be Nice guys. Thanks!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Dec 08, 2010
Posts: 1509
    
    5

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

Joined: Feb 09, 2012
Posts: 583

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 .
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10210
    
166

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.

[My Blog] [JavaRanch Journal]
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

This topic has been over so no more post reply here.thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

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.


Mohamed Sanaulla | My Blog
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instance Doubt