Can someone explain to me the difference between Float and float in java? Manythanks.
Asked
Active
Viewed 1.3k times
7
-
8Google : Wrappers vs primitives Java. – Silviu Burcea Sep 30 '13 at 12:36
-
1See the java wrapper classes http://en.wikipedia.org/wiki/Primitive_wrapper_class – Ashok kumar Sep 30 '13 at 12:37
-
It's like the difference between `int` and `Integer`, but this is less duplicated :) – Maroun Sep 30 '13 at 12:37
-
2http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html – tkroman Sep 30 '13 at 12:37
-
possible duplicate of [What is the difference between an int and an Integer in Java and C#?](http://stackoverflow.com/questions/564/what-is-the-difference-between-an-int-and-an-integer-in-java-and-c) – zch Sep 30 '13 at 12:38
2 Answers
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.
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