title: Is it possible to initialize a generic variable with a specific-typed value?
body: <p><br />
  I have a class that I want to be instantiated with either a <code>String</code> or <code>int</code> value, and I define the corresponding instance variable <code>value</code> as generic type <code>T</code>:</p>
  
  <pre><code>public class MathValue&lt;T extends Object&gt; {
      private boolean isOperand, isOperator;
  
      // a generic-typed instance variable:
      private T value;
  
      // constructor:
      public MathValue(int operand) {
  
          // compile-error -- "Incompatible types: required T, found Integer"
          this.value = new Integer(operand);
  
          this.isOperand = true;
          this.isOperator = false;
      }
  
      // constructor:
      public MathValue(String operator) {
  
          // compile-error -- "Incompatible types: required T, found String"
          this.value = operand;
  
          this.isOperand = false;
          this.isOperator = true;
      }
  }
  </code></pre>
  
  <p><br />
  I <em>could</em> very well have a single constructor instead, with a formal parameter of type <code>T</code>, but I want to enforce the class' instantiation with a <code>String</code> or <code>int</code> argument:</p>
  
  <pre><code>public class MathValue&lt;T extends Object&gt; {
      private boolean isOperand, isOperator;
  
      // a generic-typed instance variable:
      private T value;
  
      // it totally works, but does not enforce specific-typed instantiation:
      public MathValue(T operandOrOperator) {
          this.value = operandOrOperator;
  
          if (operandOrOperator instanceof Integer) {
              this.isOperand = true;
              this.isOperator = false;
  
          } else if (operandOrOperator instanceof String) {
              this.isOperand = false;
              this.isOperator = true;
          }
      }
  }
  </code></pre>
  
  <p><br />
  So despite the logical error of wanting to make a generic-typed class "not so generic", is it possible to instantiate a generic variable with a specific-typed value?
  <br /></p>