title: How to implement a generic `max(Comparable a, Comparable b)` function in Java?
body: <p>I'm trying to write a generic max function that takes two <code>Comparable</code>s.</p>
  
  <p>So far I have </p>
  
  <pre><code>public static &lt;T extends Comparable&lt;?&gt;&gt; T max(T a, T b) {
      if (a == null) {
          if (b == null) return a;
          else return b;
      }
      if (b == null)
          return a;
      return a.compareTo(b) &gt; 0 ? a : b;
  }
  </code></pre>
  
  <p>This fails to compiles with </p>
  
  <pre><code>The method compareTo(capture#5-of ?) in the type Comparable&lt;capture#5-of ?&gt; is not applicable for the arguments (T)
  </code></pre>
  
  <p>What I think this is saying is that that the <code>?</code> in <code>Comparable&lt;?&gt;</code> may be interpreted as one type for parameter a, and another for parameter b, so that they can't be compared.</p>
  
  <p>How do I dig myself out of this hole?</p>