title: How to say "both arguments must have the same type" in Java?
body: <p>In unit testing, you sometimes want a method like this one:</p>
  <pre class="lang-java prettyprint-override"><code>&lt;V&gt; void checkEq(V expectation, V value) {
    if (!expectation.equals(value))
      throw new IllegalArgumentException(&quot;not equal&quot;);
  }
  </code></pre>
  <p>(assume for a second that we can't use junit for some reason) The idea is that the compiler should ensure both arguments have <em>the</em> <em>same</em> <em>type</em>.
  However the compiler doesn't complain about this invocation, where the types obviously differ:</p>
  <pre class="lang-java prettyprint-override"><code>checkEq(1, &quot;&quot;);
  </code></pre>
  <p>I've tried some variations of this, like <code>checkEq(V expectation, Supplier&lt;V&gt; value)</code> and <code>checkEq(1, () -&gt; &quot;&quot;)</code>. It seems like the compiler is always happy to insert <code>Object</code> for <code>V</code>. Is there some way to tell the compiler not to use the trivial <code>Object</code> solution?</p>