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

Doubt with generics

Pradeep Kumar
Ranch Hand

Joined: Sep 11, 2007
Posts: 77
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


SCJP 1.6
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210



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.


SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Madhukar Ojha
Ranch Hand

Joined: Mar 21, 2007
Posts: 71
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.


SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
Pradeep Kumar
Ranch Hand

Joined: Sep 11, 2007
Posts: 77
Tested, verified and understood.

Thanks and Regards,
Pradeep
Pradeep Kumar
Ranch Hand

Joined: Sep 11, 2007
Posts: 77
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

Joined: Jul 05, 2008
Posts: 141
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


SCJP Java 6 (98%) - Story, SCJA (88%) - Story
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt with generics