File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why can't we use the IS-A test for the enhanced for-loop? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why can Watch "Why can New topic
Author

Why can't we use the IS-A test for the enhanced for-loop?

Denise Advincula
Ranch Hand

Joined: Jan 01, 2007
Posts: 160
This is a sample from the K&B book (p 559), with a little addition:



I get a compiler error (incompatible types) for this line:

for (Integer o : oa)

If I change it to for (Object o : oa), it's working fine. Integer IS-A Object but how come it is not applicable here?


SCJP/OCPJP 6 | SCWCD/OCPJWCD 5 | OCMJEA in progress...
Keith Nagle
Ranch Hand

Joined: May 06, 2008
Posts: 65
Originally posted by Denise Saulon:
This is a sample from the K&B book (p 559), with a little addition:



I get a compiler error (incompatible types) for this line:

for (Integer o : oa)

If I change it to for (Object o : oa), it's working fine. Integer IS-A Object but how come it is not applicable here?


I would imagine that you are getting the compiler error here because if may be possible that you will try to invoke methods using the Integer reference variable (such as o.intValue()) that Object class cannot do.
Consider that an array of Objects is returned when you invoke
even though you
know that it's an array of Integers. The array is only constructed at run time so at compile time the compiler wont know what the actual array type will be.
Regards


SCJP 5.0
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117

If I change it to for (Object o : oa), it's working fine. Integer IS-A Object but how come it is not applicable here?


You're telling the compiler you have an array of Objects. Next you're saying 'and with the Integer objects in the array, I'll....'. The compiler disagrees, because Object IS-NOT-AN Integer.


Java hobbyist.
Arie Prastowo
Greenhorn

Joined: Apr 20, 2008
Posts: 22
It is the same as this
Object o=new Integer(5);
Integer i=o; //--> will not compile


SCJP 5.0<br />Use the power of mind
Madhukar Ojha
Ranch Hand

Joined: Mar 21, 2007
Posts: 71
Hi Rancher



Because referenc type of oa is Type Object[] .
Hence return type of object is of type is Object.

You are trying to hold the reference type of super class in sub type .

You can o like this


I hope it will help you.


SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
Denise Advincula
Ranch Hand

Joined: Jan 01, 2007
Posts: 160
I was confused . But at least now I got it. I must have interpreted it as Integer IS-A Object but it's really the other way around.

I appreciate so much your replies guys. Thank You!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can't we use the IS-A test for the enhanced for-loop?