Package java.lang
Class Float
- java.lang.Object
-
- java.lang.Number
-
- java.lang.Float
-
- All Implemented Interfaces:
Serializable
,Comparable<Float>
,Modified
public final class Float extends Number implements Comparable<Float>, Modified
The wrapper for the primitive typefloat
.- Since:
- 1.0
- See Also:
Number
, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static int
MAX_EXPONENT
Maximum base-2 exponent that a finite value of thefloat
type may have.static float
MAX_VALUE
Constant for the maximumfloat
value, (2 - 2-23) * 2127.static int
MIN_EXPONENT
Minimum base-2 exponent that a normal value of thefloat
type may have.static float
MIN_NORMAL
Constant for the smallest positive normal value of thefloat
type.static float
MIN_VALUE
Constant for the minimumfloat
value, 2-149.static float
NaN
Constant for the Not-a-Number (NaN) value of thefloat
type.static float
NEGATIVE_INFINITY
Constant for the negative infinity value of thefloat
type.static float
POSITIVE_INFINITY
Constant for the positive infinity value of thefloat
type.static int
SIZE
Constant for the number of bits needed to represent afloat
in two's complement form.
-
Constructor Summary
Constructors Constructor Description Float(double value)
Constructs a newFloat
with the specified primitive double value.Float(float value)
Constructs a newFloat
with the specified primitive float value.Float(String string)
Constructs a newFloat
from the specified string.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description byte
byteValue()
Returns this object's value as a byte.static int
compare(float float1, float float2)
Compares the two specified float values.int
compareTo(Float object)
Compares this object to the specified float object to determine their relative order.double
doubleValue()
Returns this object's value as a double.boolean
equals(Object object)
Tests this double for equality withobject
.float
floatValue()
Gets the primitive value of this float.int
hashCode()
Returns an integer hash code for this object.int
intValue()
Returns this object's value as an int.boolean
isInfinite()
Indicates whether this object represents an infinite value.static boolean
isInfinite(float f)
Indicates whether the specified float represents an infinite value.boolean
isNaN()
Indicates whether this object is a Not-a-Number (NaN) value.static boolean
isNaN(float f)
Indicates whether the specified float is a Not-a-Number (NaN) value.long
longValue()
Returns this object's value as a long.static float
parseFloat(String string)
Parses the specified string as a float value.short
shortValue()
Returns this object's value as a short.String
toString()
Returns a string containing a concise, human-readable description of this object.static String
toString(float f)
Returns a string containing a concise, human-readable description of the specified float value.static Float
valueOf(float f)
Returns aFloat
instance for the specified float value.static Float
valueOf(String string)
-
-
-
Field Detail
-
MAX_VALUE
public static final float MAX_VALUE
Constant for the maximumfloat
value, (2 - 2-23) * 2127.- See Also:
- Constant Field Values
-
MIN_VALUE
public static final float MIN_VALUE
Constant for the minimumfloat
value, 2-149.- See Also:
- Constant Field Values
-
NaN
public static final float NaN
Constant for the Not-a-Number (NaN) value of thefloat
type.- See Also:
- Constant Field Values
-
POSITIVE_INFINITY
public static final float POSITIVE_INFINITY
Constant for the positive infinity value of thefloat
type.- See Also:
- Constant Field Values
-
NEGATIVE_INFINITY
public static final float NEGATIVE_INFINITY
Constant for the negative infinity value of thefloat
type.- See Also:
- Constant Field Values
-
MIN_NORMAL
public static final float MIN_NORMAL
Constant for the smallest positive normal value of thefloat
type.- Since:
- 1.6
- See Also:
- Constant Field Values
-
MAX_EXPONENT
public static final int MAX_EXPONENT
Maximum base-2 exponent that a finite value of thefloat
type may have. Equal toMath.getExponent(Float.MAX_VALUE)
.- Since:
- 1.6
- See Also:
- Constant Field Values
-
MIN_EXPONENT
public static final int MIN_EXPONENT
Minimum base-2 exponent that a normal value of thefloat
type may have. Equal toMath.getExponent(Float.MIN_NORMAL)
.- Since:
- 1.6
- See Also:
- Constant Field Values
-
SIZE
public static final int SIZE
Constant for the number of bits needed to represent afloat
in two's complement form.- Since:
- 1.5
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Float
public Float(float value)
Constructs a newFloat
with the specified primitive float value.- Parameters:
value
- the primitive float value to store in the new instance.
-
Float
public Float(double value)
Constructs a newFloat
with the specified primitive double value.- Parameters:
value
- the primitive double value to store in the new instance.
-
Float
public Float(String string) throws NumberFormatException
Constructs a newFloat
from the specified string.- Parameters:
string
- the string representation of a float value.- Throws:
NumberFormatException
- ifstring
can not be parsed as a float value.- See Also:
parseFloat(String)
-
-
Method Detail
-
parseFloat
public static float parseFloat(String string) throws NumberFormatException
Parses the specified string as a float value.- Parameters:
string
- the string representation of a float value.- Returns:
- the primitive float value represented by
string
. - Throws:
NumberFormatException
- ifstring
can not be parsed as a float value.- Since:
- 1.2
- See Also:
valueOf(String)
-
compareTo
public int compareTo(Float object)
Compares this object to the specified float object to determine their relative order. There are two special cases:Float.NaN
is equal toFloat.NaN
and it is greater than any other float value, includingFloat.POSITIVE_INFINITY
;- +0.0f is greater than -0.0f
- Specified by:
compareTo
in interfaceComparable<Float>
- Parameters:
object
- the float object to compare this object to.- Returns:
- a negative value if the value of this float is less than the
value of
object
; 0 if the value of this float and the value ofobject
are equal; a positive value if the value of this float is greater than the value ofobject
. - Since:
- 1.2
- See Also:
Comparable
-
compare
public static int compare(float float1, float float2)
Compares the two specified float values. There are two special cases:Float.NaN
is equal toFloat.NaN
and it is greater than any other float value, includingFloat.POSITIVE_INFINITY
;- +0.0f is greater than -0.0f
- Parameters:
float1
- the first value to compare.float2
- the second value to compare.- Returns:
- a negative value if
float1
is less thanfloat2
; 0 iffloat1
andfloat2
are equal; a positive value iffloat1
is greater thanfloat2
. - Since:
- 1.4
-
isInfinite
public boolean isInfinite()
Indicates whether this object represents an infinite value.- Returns:
true
if the value of this float is positive or negative infinity;false
otherwise.
-
isInfinite
public static boolean isInfinite(float f)
Indicates whether the specified float represents an infinite value.- Parameters:
f
- the float to check.- Returns:
true
if the value off
is positive or negative infinity;false
otherwise.
-
isNaN
public boolean isNaN()
Indicates whether this object is a Not-a-Number (NaN) value.- Returns:
true
if this float is Not-a-Number;false
if it is a (potentially infinite) float number.
-
isNaN
public static boolean isNaN(float f)
Indicates whether the specified float is a Not-a-Number (NaN) value.- Parameters:
f
- the float value to check.- Returns:
true
iff
is Not-a-Number;false
if it is a (potentially infinite) float number.
-
equals
public boolean equals(Object object)
Tests this double for equality withobject
. To be equal,object
must be an instance ofFloat
andfloatToIntBits
must give the same value for both objects.Note that, unlike
==
,-0.0
and+0.0
compare unequal, andNaN
s compare equal by this method.- Overrides:
equals
in classObject
- Parameters:
object
- the object to compare this float with.- Returns:
true
if the specified object is equal to thisFloat
;false
otherwise.- See Also:
Object.hashCode()
-
hashCode
public int hashCode()
Description copied from class:Object
Returns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)
returnstrue
must return the same hash code value. This means that subclasses ofObject
usually override both methods or neither method.Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCode
method if you intend implementing your ownhashCode
method.- Overrides:
hashCode
in classObject
- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
toString
public String toString()
Description copied from class:Object
Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data.
-
toString
public static String toString(float f)
Returns a string containing a concise, human-readable description of the specified float value.- Parameters:
f
- the float to convert to a string.- Returns:
- a printable representation of
f
.
-
valueOf
public static Float valueOf(String string) throws NumberFormatException
- Throws:
NumberFormatException
-
valueOf
public static Float valueOf(float f)
Returns aFloat
instance for the specified float value.- Parameters:
f
- the float value to store in the instance.- Returns:
- a
Float
instance containingf
. - Since:
- 1.5
-
byteValue
public byte byteValue()
Description copied from class:Number
Returns this object's value as a byte. Might involve rounding and/or truncating the value, so it fits into a byte.
-
shortValue
public short shortValue()
Description copied from class:Number
Returns this object's value as a short. Might involve rounding and/or truncating the value, so it fits into a short.- Overrides:
shortValue
in classNumber
- Returns:
- the primitive short value of this object.
-
intValue
public int intValue()
Description copied from class:Number
Returns this object's value as an int. Might involve rounding and/or truncating the value, so it fits into an int.
-
longValue
public long longValue()
Description copied from class:Number
Returns this object's value as a long. Might involve rounding and/or truncating the value, so it fits into a long.
-
floatValue
public float floatValue()
Gets the primitive value of this float.- Specified by:
floatValue
in classNumber
- Returns:
- this object's primitive value.
-
doubleValue
public double doubleValue()
Description copied from class:Number
Returns this object's value as a double. Might involve rounding.- Specified by:
doubleValue
in classNumber
- Returns:
- the primitive double value of this object.
-
-