This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt with generics

 
Pradeep Kumar
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In generics as we have generic delcarations can someone explain me how to interpret the below method declaration. I am a bit confused with this regarding what can be the input values and waht should be the output values.

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

Thanks and Regards,
Pradeep
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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


<E extends Number> means generic parameter E can be anything which extends Number like Integer, Long, Float.

so we can call method process using following parameters.



List<? super E> means return type can be List of E or super type of E
so return type can be list of Number objects or list of Object objects.
 
Madhukar Ojha
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rancher,

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

1. parmeter must be of type List and its <type> must be of type Number
or sub type of Number.

2. Return type must be of type List or its sub type and its <type>
must be of type Number or its super type.(Object in this case.)

I hope this will help you.
 
Pradeep Kumar
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tested, verified and understood.

Thanks and Regards,
Pradeep
 
Pradeep Kumar
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But that still leaves one doubt unanswered or rather one doubt araises.

public static <E super String> void process(List<E> nums){}

Why does the compiler complain about <E super String> in the above statement.

Thanks and Regards,
Pradeep
 
Raphael Rabadan
Ranch Hand
Posts: 141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pradeep Kumar:
But that still leaves one doubt unanswered or rather one doubt araises.

public static <E super String> void process(List<E> nums){}

Why does the compiler complain about <E super String> in the above statement.

Thanks and Regards,
Pradeep



As far as I know, super only can be used with wild cards.

Kind Regards,
Raphael Rabadan
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic