Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Another Java Inquisition Quesion  RSS feed

 
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9613
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9613
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But isn't Integer a subclass of Number..?
so then (Integer) aNumber is an upcast..
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!