7 Replies Latest reply on Nov 25, 2013 11:11 AM by TPD-Opitz

    cast Number to unknown number type

    Gen.Java

      Hi all,

      I need to convert an object to double, increment it, and then convert the double back to object (preserving the original object class). This object class could be byte, int, long, short, float, or double.

       

      <code>

      Object o=123;

      double d;

      Number n;

      Class c;

       

      c=o.getClass();

      n=(Number)o;

      d=n.doubleValue();

      d++;

      n=d;

      o=c.cast(n);

      <code>

      The last statement produces ClassCastException.

       

      why the <code> tag did not work. I miss the "view" button in the old "Sun" forum.