Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In the generics section of Bloch's Effective Java (which handily is the "free" chapter available to all: http://java.sun.com/docs/books/effective/generics.pdf), he says:

If a type parameter appears only once in a method declaration, replace it with a wildcard.

(See page 31-33 of that pdf)

The signature in question is:

public static void swap(List<?> list, int i, int j)

vs

public static void swap(List<E> list, int i, int j)

And then proceeds to use a static private "helper" function with an actual type parameter to perform the work. The helper function signature is EXACTLY that of the second option.

Why is the wildcard preferable, since you need to NOT use a wildcard to get the work done anyway? I understand that in this case since he's modifying the List and you can't add to a collection with an unbounded wildcard, so why use it at all?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

Why is the wildcard preferable

I believe Josh states it clearly:

In a public API, the [wildcard] is better because it’s simpler.

That is, for the users of the API. The small extra complexity in the implementation is not visible from outside. And as the implementation is written only once, but the API may be used many times by many different people, simplifying the API at the cost of making the implementation slightly more complex is still an overall win.

> why use it at all?

You mean, why is it better than

public static void swap(List list, int i, int j)

? Because the latter would forfeit generics altogether, letting the compiler omit all generic type checking, and allowing us to write sloppy code. I think it is better to use generic type signatures even if they are this general, just to keep up the habit.

Update reflecting on the comment below

You forgot the generic parameter declaration from the second signature, correctly it should be

public static <E> void swap(List<E> list, int i, int j)

That is, <E> appears twice, for no added benefit, vs <?> appearing only once in the first signature. This is why it is simpler.

share|improve this answer
1  
No, why is swap(List<?>, ...) preferable to swap(List<E>, ...). I fail to see why it's simpler, in any context. –  Michael Campbell Sep 4 '11 at 22:15
    
@Michael, see my update. –  Péter Török Sep 4 '11 at 22:40
1  
Don't really see the benefit. For a user to use the swap method, a call to either version public static <E> void swap(List<E> list, int i, int j) or public static void swap(List<?> list, int i, int j) would lokk exactly the same, namely swap(myList, 0, 1). So whether the <E> appears twice or having a wildcard instead doesn't really matter in my opinion. Maybe I'm missing something? –  Will Nov 11 '12 at 22:58
1  
@Will, prior to calling, you (usually) read the API declaration. The wildcard version of the declaration is (marginally) simpler for our brains to interpret. –  Péter Török Nov 12 '12 at 8:40
1  
ok, so it's really about this tiny simplification. thanks. –  Will Nov 12 '12 at 10:41
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.