Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Float and float in java? [closed]

Tags:

java

variables

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

like image 608
John Paul Avatar asked Sep 30 '13 12:09

John Paul


2 Answers

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.

like image 131
Steve B. Avatar answered Oct 29 '22 02:10

Steve B.


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.

like image 45
Nathaniel Johnson Avatar answered Oct 29 '22 02:10

Nathaniel Johnson