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 intMAX_EXPONENTMaximum base-2 exponent that a finite value of thefloattype may have.static floatMAX_VALUEConstant for the maximumfloatvalue, (2 - 2-23) * 2127.static intMIN_EXPONENTMinimum base-2 exponent that a normal value of thefloattype may have.static floatMIN_NORMALConstant for the smallest positive normal value of thefloattype.static floatMIN_VALUEConstant for the minimumfloatvalue, 2-149.static floatNaNConstant for the Not-a-Number (NaN) value of thefloattype.static floatNEGATIVE_INFINITYConstant for the negative infinity value of thefloattype.static floatPOSITIVE_INFINITYConstant for the positive infinity value of thefloattype.static intSIZEConstant for the number of bits needed to represent afloatin two's complement form.
-
Constructor Summary
Constructors Constructor Description Float(double value)Constructs a newFloatwith the specified primitive double value.Float(float value)Constructs a newFloatwith the specified primitive float value.Float(String string)Constructs a newFloatfrom the specified string.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description bytebyteValue()Returns this object's value as a byte.static intcompare(float float1, float float2)Compares the two specified float values.intcompareTo(Float object)Compares this object to the specified float object to determine their relative order.doubledoubleValue()Returns this object's value as a double.booleanequals(Object object)Tests this double for equality withobject.floatfloatValue()Gets the primitive value of this float.inthashCode()Returns an integer hash code for this object.intintValue()Returns this object's value as an int.booleanisInfinite()Indicates whether this object represents an infinite value.static booleanisInfinite(float f)Indicates whether the specified float represents an infinite value.booleanisNaN()Indicates whether this object is a Not-a-Number (NaN) value.static booleanisNaN(float f)Indicates whether the specified float is a Not-a-Number (NaN) value.longlongValue()Returns this object's value as a long.static floatparseFloat(String string)Parses the specified string as a float value.shortshortValue()Returns this object's value as a short.StringtoString()Returns a string containing a concise, human-readable description of this object.static StringtoString(float f)Returns a string containing a concise, human-readable description of the specified float value.static FloatvalueOf(float f)Returns aFloatinstance for the specified float value.static FloatvalueOf(String string)
-
-
-
Field Detail
-
MAX_VALUE
public static final float MAX_VALUE
Constant for the maximumfloatvalue, (2 - 2-23) * 2127.- See Also:
- Constant Field Values
-
MIN_VALUE
public static final float MIN_VALUE
Constant for the minimumfloatvalue, 2-149.- See Also:
- Constant Field Values
-
NaN
public static final float NaN
Constant for the Not-a-Number (NaN) value of thefloattype.- See Also:
- Constant Field Values
-
POSITIVE_INFINITY
public static final float POSITIVE_INFINITY
Constant for the positive infinity value of thefloattype.- See Also:
- Constant Field Values
-
NEGATIVE_INFINITY
public static final float NEGATIVE_INFINITY
Constant for the negative infinity value of thefloattype.- See Also:
- Constant Field Values
-
MIN_NORMAL
public static final float MIN_NORMAL
Constant for the smallest positive normal value of thefloattype.- 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 thefloattype 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 thefloattype 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 afloatin two's complement form.- Since:
- 1.5
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Float
public Float(float value)
Constructs a newFloatwith the specified primitive float value.- Parameters:
value- the primitive float value to store in the new instance.
-
Float
public Float(double value)
Constructs a newFloatwith 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 newFloatfrom the specified string.- Parameters:
string- the string representation of a float value.- Throws:
NumberFormatException- ifstringcan 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- ifstringcan 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.NaNis equal toFloat.NaNand it is greater than any other float value, includingFloat.POSITIVE_INFINITY;- +0.0f is greater than -0.0f
- Specified by:
compareToin 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 ofobjectare 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.NaNis equal toFloat.NaNand 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
float1is less thanfloat2; 0 iffloat1andfloat2are equal; a positive value iffloat1is greater thanfloat2. - Since:
- 1.4
-
isInfinite
public boolean isInfinite()
Indicates whether this object represents an infinite value.- Returns:
trueif the value of this float is positive or negative infinity;falseotherwise.
-
isInfinite
public static boolean isInfinite(float f)
Indicates whether the specified float represents an infinite value.- Parameters:
f- the float to check.- Returns:
trueif the value offis positive or negative infinity;falseotherwise.
-
isNaN
public boolean isNaN()
Indicates whether this object is a Not-a-Number (NaN) value.- Returns:
trueif this float is Not-a-Number;falseif 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:
trueiffis Not-a-Number;falseif it is a (potentially infinite) float number.
-
equals
public boolean equals(Object object)
Tests this double for equality withobject. To be equal,objectmust be an instance ofFloatandfloatToIntBitsmust give the same value for both objects.Note that, unlike
==,-0.0and+0.0compare unequal, andNaNs compare equal by this method.- Overrides:
equalsin classObject- Parameters:
object- the object to compare this float with.- Returns:
trueif the specified object is equal to thisFloat;falseotherwise.- See Also:
Object.hashCode()
-
hashCode
public int hashCode()
Description copied from class:ObjectReturns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually 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
hashCodemethod if you intend implementing your ownhashCodemethod.- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
toString
public String toString()
Description copied from class:ObjectReturns 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 aFloatinstance for the specified float value.- Parameters:
f- the float value to store in the instance.- Returns:
- a
Floatinstance containingf. - Since:
- 1.5
-
byteValue
public byte byteValue()
Description copied from class:NumberReturns 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:NumberReturns this object's value as a short. Might involve rounding and/or truncating the value, so it fits into a short.- Overrides:
shortValuein classNumber- Returns:
- the primitive short value of this object.
-
intValue
public int intValue()
Description copied from class:NumberReturns 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:NumberReturns 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:
floatValuein classNumber- Returns:
- this object's primitive value.
-
doubleValue
public double doubleValue()
Description copied from class:NumberReturns this object's value as a double. Might involve rounding.- Specified by:
doubleValuein classNumber- Returns:
- the primitive double value of this object.
-
-