1

Is there any difference between the following two declarations?

public<C extends Condition<E>> List<E> search (C condition)

public List<E> search (Condition<E> condition)

One difference is obvious: in the first case C can be used in the body of search. But assumed that C would not be used in the body of search: is there still any difference?

2
  • 6
    Yes, there is - the second one is much easier to read :-) :-) :-) Commented Nov 12, 2013 at 16:19
  • 1
    Even if C is used in the body of search, there is no difference in terms of functionality from the perspective of outside code, since the second search can still just call the first search internally Commented Nov 13, 2013 at 2:06

1 Answer 1

1

No, there's no useful difference. The distinction could be simplified to the following.

<T> void m(T object)

void m(Object object)

Although with the first one could call this.<String>m(42) and it wouldn't compile - but there's no value to that.

The value of a generic method comes when there is some relationship expressed by its type parameters, for example:

<T> T giveItBackToMe(T object) {
    return object;
}

...

String s = giveItBackToMe("asdf");
Integer i = giveItBackToMe(42);

Or:

<T> void listCopy(List<T> from, List<? super T> to) {
    to.addAll(from);
}

...

List<Integer> ints = Arrays.asList(1, 2, 3);
List<Number> nums = new ArrayList<>();
listCopy(ints, nums);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.