aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes enhanced for loop and generics 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 "enhanced for loop and generics" Watch "enhanced for loop and generics" New topic
Author

enhanced for loop and generics

Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1



Can someone explain me that?


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

May I explain to myself?

list reference can be assigned to any List that has generic type extending Number.
If it would be a list of Double - Double can not be casted to Integer.

Am I right?
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
I'd say so.

List <? extends X> is a way of saying "This list can be assigned a list of X or X subtypes and nothing can be added to it". So you don't try to add a Double to an Integer List, a bit like in your in your example.
Also because you don't know what types are actually in there, just that they extend X, the only thing you can safely get out is a type X. The compiler can see this and throws a wobbly if you try anything else.


SCJP6
Sonali Sehgal
Ranch Hand

Joined: Jul 09, 2009
Posts: 75
List<? extends Number>

Try understanding it like this: A List containing instances of Number or its subclass(es). This will allow you to retrieve Number objects

because the compiler knows that this list contains objects that can be assigned to a variable of class Number. However, you cannot

add any object to the list because the compiler doesn't know the exact class of objects contained by the list so it cannot check

whether whatever you are adding is eligible to be added to the list or not.

List<? super Number>

Something like this: A List containing instances of Number or its super class(es). Thus, this will allow you to do this:

list.add(new Integer(10)); because Integer can be assigned to a variable of type Number or its super class, but it will not allow

you to retrieve anything other than Object: Object obj = list.get(i); because the compiler doesn't know the exact class of objects

contained by list.

List<?>

This is same as List<? extends Object>. Try understanding like this: A List containing instances of some class that extends Object

class. Thus, this will not allow you to add anything to it because the compiler doesn't know the exact class of objects contained

by the list so it cannot check whether whatever you are adding is eligible to be added to the list or not. Further, this will only

allow you to do this: Object o = list.get(index); because all the compiler knows is that this list contains objects.


For enhanced for loop you can refer to Kathy and seirra on page 350 and 351

I hope this explanation makes your doubt clear
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: enhanced for loop and generics