Search code examples
javatypesfloating-pointprimitive

Is there any difference between these two statements?


  1. float ff = 1.2f;
  2. Float fo = new Float(1.2f);
  3. double fg = 3.2d;
  4. Double fh = new Double(2.1d);

Can I use '=' between the (1) and (3) or between the (2) and (4)??


Solution

  • Yes.

    1. Makes a plain old data type (AKA a primitive type) called "float."
    2. Makes a Java Object called Float that holds that value that happens to be identical to (1)

    Responding to the edit questions:

    You will see

    1. "possible loss of precision" message if you try ff = fg.
    2. "incompatible types" if you try fo = fh.
    3. fg = ff will work fine (the float fits in a double).
    4. fh = fo will still give you an "incompatible types".