7

Can someone explain to me the difference between Float and float in java? Manythanks.

John Paul
  • 772
  • 1
  • 6
  • 17

2 Answers2

11

Float is an object; float is a primitive. Same relationship as Integer and int, Double and double, Long and long.

float can be converted to Float by autoboxing, e.g.

float f=1.0f;
Float floatObject = f;

or explicitly

Float floatObject = new Float(f);

Initially primitives were retained alongside the object versions for speed. Autoboxing/unboxing was added with java 5 to facilitate conversion.

zakinster
  • 10,508
  • 1
  • 41
  • 52
Steve B.
  • 55,454
  • 12
  • 93
  • 132
6

Float is a class which wraps the primitive float. In newer versions of Java, a feature called autoboxing makes it hard to tell that they are different but generally speaking, use float when you using the number to do calculations and Float when you need to store it in Object collections.

Nathaniel Johnson
  • 4,731
  • 1
  • 42
  • 69