Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Capture Conv: rev/reverse - what's the point?

843793Mar 1 2005 — edited Mar 15 2005
Take this example from http://www.langer.camelot.de/GenericsFAQ/FAQSections/TechnicalDetails.html
public static List<?> reverse(List<?> list) { return rev(list); }
private static <T> List<T> rev(List<T> list) {
	List<T> tmp = new ArrayList<T>(list);
	for (int i = 0; i < list.size(); i++)
		list.set(i, tmp.get(list.size() - i - 1);
	return tmp;
}
Now, I've read the JLS, the Generics tutorial, the JOT.fm article and the langer.camelot.de FAQ, but I can't for the life of me figure out one thing:

What's the point!? The wildcard version of reverse returns List<?> whereas the naive alternative (public static <T> List<T> reverse(List<T> list)) returns whatever you pass in. That seems much more useful! You pass in a List<String> and you get back a List<String> The JLS says that the templatized version "is undesirable, as it exposes implementation information to the caller."

My question is this: is that the real reason? It's not, right? What implementation information is exposed? The type parameter? How is that bad? Is it bad enough to reduce the functionality of the method?

I have a hunch that "exposing implementation information" is low on the tradeoff scale - and that the real reason for the wildcard version is backward compatibility. If you only had the templatized version, old code that tries to pass in a plain-old List to reverse would now generate unchecked warnings because of the conversion from raw List to the formal parameter's type List<T>. The preferred wildcard version doesn't generate unchecked warnings because the conversion from raw List to List<?> is 'safe'.

Isn't that right? The reason that "public static <T> List<T> reverse(List<T> list)" is bad is not because it exposes implementation details, but because it causes old code to generate unchecked warnings, non?

If I'm right, then my follow up question would be: who cares? Who cares if old code generates unchecked warnings? Isn't that fair? - a sort of gentle pseudo-deprecation of raw types?

And furthermore, if you'll allow me to smush several questions together, why the preoccupation with eliminating unchecked conversions all over the place? Doesn't the bytecode that comes out look the same either way? What benefit does an unchecked-conversion-free program or CompilationUnit have over one with such conversions? Does provability make possible some future JVM optimizations where some casts or instanceofs can be eliminated? I don't really see how, unless we're talking about some wacky static compiler. Why go to so much trouble to avoid unchecked conversions?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 12 2005
Added on Mar 1 2005
7 comments
147 views