title: Cannot cast java.lang.Float to float when using Class.cast?
body: <p>I already have a solution, but I don't like it. Also, it would be nice to know your thoughts about this.</p>
  
  <p>You see, <code>elem.get</code> gives back an <code>Object</code>, that is going to be a <code>Float</code> in the case of x and y.</p>
  
  <p>This code throws the Exception described in the title:</p>
  
  <pre><code>String names[] = { "x", "y", "src" };
  Class types[] = { float.class, float.class, String.class };
  
  for (int i = 0; i &lt; names.length; i++)
  {
      if (elem.containsKey(names[i]))
      {
          par.add(types[i]);
          arg.add(types[i].cast(elem.get(names[i])));
      }
  }
  </code></pre>
  
  <p>The code is used to define the list of parameters to instantiate an object. Both the parameters and the object's class name are defined in an external data file (but nevermind about that).</p>
  
  <p>The solution, something I'm not fond of (because it loses the flexibility I was trying to attain), has this within the for:</p>
  
  <pre><code>if(types[i] == float.class)
  {
      float v = (Float)elem.get(names[i]);
      arg.add(v);
  } else
  {
      arg.add(types[i].cast(elem.get(names[i])));
      break;
  }
  </code></pre>
  
  <p>Other option would be changing <code>float.class</code> to <code>Float.class</code> in <code>types</code>, but I won't do that because that code is used to instantiate objects which have <code>float</code> parameters in their constructors.</p>
  
  <p>Thanks!</p>