• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Pls help with this error in generics

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;
class GenLearn
{
public static void main( String args[] )
{
ArrayList<Integer> input = new ArrayList<Integer>();
input.add(5);
List<Integer> output ;//= new ArrayList<Integer>();
output = process( input ); //error!!

}
public static <E extends Number> List<? super E> process( List<E> nums)
{
List<Integer> tp = new ArrayList<Integer>();
tp.add(6);
return tp; // error!!

}
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tricky.

output = process( input ); //error!!


because output is of type List<Integer> and process returns <? super Integer> when invoked with a List<Integer>.
You cannot assign like this;
List<Integer> li_int = null;
List<? super Integer> li_sup = null;
li_int = li_sup; // not allowed

Because li_sup could also refer to a List<Object>.




In the method:

public static <E extends Number> List<? super E> process( List<E> nums)
{...


return type depends on the type the list is invoked with.
You can easily invoke this method with a List<Float>.
So it is not allowed to return a List<Integer> because Integer is not a Float itself nor a supertype thereof.


Note that it doesn't depend on how you invoke the process method in your main method. It has to be consistant on all possible invokations.


As I said in the beginning:
Tricky.


Yours,
Bu.
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think I have ever seen syntax like this: <E extends Number> List<? super E>. On both side of List? Is that legal? What does that mean?
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
legal? yes.


public static <E extends Number> List<? super E> process( List<E> nums) {
...
}




the first <E extends Number> says only that the method is generic and can be used for all types that are either Number itself or subtypes (Integer, Float...).
It does not belong to the return type.

The second <? super E> however belongs to the return type List that is parameterized.
So you won't expect to get lists back that contain String etc., only Lists of the kind how the list is invoked. Or supertypes.


Bu.
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic