*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Another Java Inquisition Quesion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Another Java Inquisition Quesion" Watch "Another Java Inquisition Quesion" New topic
Author

Another Java Inquisition Quesion

Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

Hi all..

public static void main( String args[] )
{
List<? extends Number> type = new ArrayList<Integer>(); // 1
for ( Integer n : type ) // 2
{
System.out.println(n); // 3
}
}
public <T> void seth(List<?> type) // 4
{
type.add("hi"); // 5
}

Why is it that the foreach loop does not work??Surely it can iterate over the Integers in the list since Integer extends Number??

Also I don't understand the following:

public <T> void seth(List<?> type)

I understand that T is defining the parameters,but how does this work in conjunction with the <?>...I thought these were kind of mutually exclusive....

Sorry I can't use CODE tags....javaranch must be under construction or something


===>SCJP 1.5(72%)<===
==>SCWCD1.5(76%)<===
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

the loop doesn't work as the list is accessed through a reference of type <? extends Number>. So the return values will be of type Number. So the loop would try to assign a Number to an Integer. It is true that the actual List is of type Integer, but the compiler would check the loop by the reference whose type is <? extends Number> not <Integer>. So basically you are trying to do something like this

Integer i = (Number)list.get(0);

This would not compile as you know. Also you CAN use wildcard and Identifier form of together...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

So the for loop doesn't work because the iterator is trying to perform an upcast??That's interesting

I still don't understand the purpose of:

public <T> void seth(List<?> type)

Why not just use:

public void seth(List<?> type)

Surely they are the same?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
It is used for trick purpose only.
For this method they are same, as you are not using T inside the method.
If you have to use T inside the method then they are different.


SCJP 6
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Originally posted by Duran Harris:
So the for loop doesn't work because the iterator is trying to perform an upcast??That's interesting


The loop is trying to perform a downcast. upcasts are allowed implicitly but downcast require an explicit cast...
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

But isn't Integer a subclass of Number..?
so then (Integer) aNumber is an upcast..
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
But in your question, type is a Number and you need to convert it in Integer.

You require to do:

for ( Number n : type ) // 2
{
System.out.println(n); // 3
}

[ December 30, 2008: Message edited by: punit singh ]
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

Ah I wasn't concentrating..I can see now that it's an upcast.

So in any foreach loop...if you have for(someType a: collection)
the foreach will perform a cast- like (someType) a?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Another Java Inquisition Quesion