title: How do I use getClass() to tell if an Object is numeric?
body: <p>I want to dynamically tell if an Object in Java is numeric or not. My code is as the following:</p>
  
  <pre><code>if(obj.getClass().equals(Number.class)) {
      attributeTypeStr = "Numeric";
  } else {
      attributeTypeStr = "Non-Numeric";
  }
  </code></pre>
  
  <p>The <code>Object obj</code> can be any numeric types like <code>Integer, Float, Double</code>, etc. But when I tested it with <code>Integer/Float/Double</code>, the <code>attributeTypeStr</code> always returns <code>"Non-Numeric"</code>. I guess that's because <code>Integer/Float/Double</code> are subclasses of <code>Number</code> but <code>Number.class</code> does not equal <code>Double.class</code>. I could use something like</p>
  
  <pre><code>if(obj.getClass().equals(Integer.class) || obj.getClass().equals(Float.class) || obj.getClass().equals(Double.class))
  </code></pre>
  
  <p>But it looks too verbose. So is there a simple way to do that? </p>