title: How to return an object from method to calculate with it?
body: <p>I want to do this: <code>sum += castObjectToNumber(value)</code> and <code>castObjectToNumber</code> should look somehow like this:</p>
  
  <pre><code>private Number castObjectToNumber(Object value) {
      if(value instanceof Integer) {
          return (int)value;
      } else if(value instanceof Float) {
          return (float) value;
      } else if(value instanceof Double) {
          return (double) value;
      } else if(value instanceof Long) {
          return (long) value;
      } else if(value instanceof Byte) {
          return (byte) value;
      } else if(value instanceof Short) {
          return (short) value;
      }
  }
  </code></pre>
  
  <p>I can't change that I get a value of type Object and I want this to be a general purpose solution. My problem here is that if I return a Number object, as this is the only thing all these types have in common, I can't use the <code>+=</code> operator with it. So how do I do this in Java? I think I do choose a wrong return type and thus it won't work but I'm not sure what is the right one.</p>