title: How do I write generic Java method and compare two variables of the generic type inside the method?
body: <p>I have written the following code:</p>
  
  <pre><code>private static &lt;T&gt; T getMax(T[] array) {
          if(array.length == 0) {
              return null;
          }
  
          T max = array[0];
          for (int i = 1; i &lt; array.length; i++) {
              if (array[i] &gt; max)
                  max = array[i];
          }
          return max;
      }
  </code></pre>
  
  <p>The problem is in this line: <code>if(array[i] &gt; max)</code>. </p>
  
  <p>I understand that Java can't understand the <code>&gt;</code> operator in case of unknown/arbitrary classes. </p>
  
  <p>At the same time, I don't want to write different methods for the objects of the classes that I know I'll be sending. </p>
  
  <p><strong><em>Is there a workaround?</em></strong></p>