Saturday, 31 August 2013

Invoking generic type

Invoking generic type

I am currently following the examples on Java's Generics on the following
page: http://docs.oracle.com/javase/tutorial/java/generics/types.html
public interface Pair<K, V> {
public K getKey();
public V getValue();
}
public class OrderedPair<K, V> implements Pair<K, V> {
private K key;
private V value;
public OrderedPair(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() { return key; }
public V getValue() { return value; }
}
Pair<String, Integer> p1 = new OrderedPair<String, Integer>("Even", 8);
OrderedPair<String, Integer> p1 = new OrderedPair<>("Even", 8);
My question is about the lines at the end which create variables. At the
very start, one starts with the pair interface, while another starts with
the OrderedPair class. Is there a difference whether which one is used?
I'm just trying to understand why the code is different.

No comments:

Post a Comment