Java: Should I use float or float?

My colleague told me that I should use float whenever possible to reduce object creation and improve performance. Java silently converts a float to a Float (this requires some processing power) when necessary. Therefore, it seems to me that the only need for Float is when you need to use the Float object very often, rather than its primitive.

When you look at java.awt.color, it uses Float, perhaps unnecessarily.

When do you need to prefer Float over float in Java?

+7
source share
1 answer

Object Float can be set to null to represent an unknown value.
primitive Float guaranteed to matter.

There is some overhead in auto boxing, but this is not significant. You still have to make room for the primitive so that you don't get anything.

+7
source

All Articles