1

This is a beginner's question. Regarding declaring a generic method, what is the point of have a type parameter in front of the return type? Let me simply quote the example from Oracle Java tutorial

public class Util {
  public static <K, V> boolean compare(Pair<K, V> p1, Pair<K, V> p2) {
    return p1.getKey().equals(p2.getKey()) &&
           p1.getValue().equals(p2.getValue());
    }
}

Let's say since the types of the parameters have been declared inside the method parameter list, i.e., Pair<K, V> p1 and Pair<K, V> p2, and we expect the method to return a value of boolean type. As I suppose that we care mostly about the return value, why do we still have to explicitly insert a type parameter <K, V> in the method declaration?

3
  • @M.leRutte How is this a duplicate? Commented Oct 12, 2017 at 8:20
  • 1
    @M. le Rutte I respectfully disagree with your suggestion that my question is a duplicate. Commented Oct 12, 2017 at 8:25
  • I agree. Concluded too fast. Commented Oct 12, 2017 at 10:39

2 Answers 2

1

why do we still have to explicitly insert a type parameter <K, V> in the method declaration?

We don't have to.

First of all, you don't necessarily need type parameters at all. You could write:

public static boolean compare(Entry p1, Entry p2) {
    return p1.getKey().equals(p2.getKey()) && p1.getValue().equals(p2.getValue());
}

But if Entry is generic, you'll get "References to generic type ... should be parameterized."

If you want to avoid the warning you could then do:

public static boolean compare(Entry<?, ?> p1, Entry<?, ?> p2) {
    return p1.getKey().equals(p2.getKey()) && p1.getValue().equals(p2.getValue());
}

Which is totally fine, does not have <K, V> and gives no raw type warning. So why would you need <K, V> there? The point of <K, V> in this case may be to ensure that keys in the Entry are of the same type K and values of the same type V (i.e. assignment-compatible to it). It does not make a big difference technically in this case (equals would work the same way no matter what), but it might help to ensure you're not compare apples with bananas.

Sign up to request clarification or add additional context in comments.

2 Comments

Good answer much appreciated. Then what would be the type parameter to use in the following case, say public static boolean someMethod(Entry1<K, V> p1, Entry2<U, V> p2), meaning a method accepts two different generic types which may have some members that can be, say, compared.
@Xavier Come again?
1

Your method has two arguments, p1 and p2, both of which use the type parameters K and V. By doing this, the compiler helps you to make sure that both arguments actually use the same generic type.

If you do not need that you could change your method signature to

public static boolean compare(Pair<?, ?> p1, Pair<?, ?> p2)

but then you would be able to call compare with a pair of <String, Integer> and a pair of <Boolean, Double> which will never be equal.

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.