Can someone explain to me the difference between Float and float in java? Manythanks.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With