title: How can I simulate Haskell's "Either a b" in Java
body: <p>How can I write a typesafe Java method that returns either something of class a or something of class b? For example:</p>
  
  <pre><code>public ... either(boolean b) {
    if (b) {
      return new Integer(1);
    } else {
      return new String("hi");
    }
  }
  </code></pre>
  
  <p>What is the cleanest way?</p>
  
  <p>(   The only thing that comes to my mind is using exceptions which is obviously bad, as it is abusing a error-handling mechanism for a general language feature ...</p>
  
  <pre><code>public String either(boolean b) throws IntException {
    if (b) {
      return new String("test");
    } else {
      throw new IntException(new Integer(1));
    }
  }
  </code></pre>
  
  <p>)</p>