title: Wildcards in java generics
body: <p>I am given the Pair.java class and have to implement the PairTools.java class.</p>
  
  <p>Pair.java</p>
  
  <pre><code>import java.util.Objects;
  
  public class Pair&lt;A, B&gt; {
  
      public final A a;
      public final B b;
  
      public Pair(A a, B b) {
          this.a = a;
          this.b = b;
      }
  
      @Override
      public String toString() {
          // appending things to the empty string prevents us from having to worry about null
          // and calling toString explicitly, Objects.toString(a) + " " + Objects.toString(b)
          // would also work
          return "" + a + " " + b;
      }
  
      @Override
      public boolean equals(Object obj) {
          // `obj instanceof Pair` will automatically return false if obj is null
          if (!(obj instanceof Pair)) {
              return false;
          }
  
          // some warnings with generics are unavoidable
          @SuppressWarnings("unchecked")
          Pair&lt;A, B&gt; p = (Pair&lt;A, B&gt;) obj;
  
          // we use Objects.equals() to handle nulls easily
          return Objects.equals(a, p.a) &amp;&amp; Objects.equals(b, p.b);
      }
  
      @Override
      public int hashCode() {
          // we use Objects.hashCode() to handle nulls easily, 
          // the operation ^ is XOR, not exponentiation
          return Objects.hashCode(a) ^ Objects.hashCode(b);
      }
  }
  </code></pre>
  
  <p>In PairTools.java I have to implement the following method:</p>
  
  <pre><code>public class PairTools {
  
      /**
       * this is how you can use wildcards in generics
       * 
       * @param pair (assume never null)
       * @return a pair containing two references to a of the given pair
       */
      public static &lt;A&gt; Pair&lt;A, A&gt; copyA(Pair&lt;A, ?&gt; pair) {
          return null;
      }
  
  }
  </code></pre>
  
  <p>I don't understand the implementation. I need an explanation.</p>