Package java.lang
Class Float
java.lang.Object
java.lang.Number
java.lang.Float
- All Implemented Interfaces:
Serializable,Comparable<Float>
public final class Float extends Number implements Comparable<Float>
The wrapper for the primitive type
float.- 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.static Class<Float>TYPETheClassobject that represents the primitive typefloat. -
Constructor Summary
-
Method Summary
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.static intfloatToIntBits(float value)Returns an integer corresponding to the bits of the given IEEE 754 single precision floatvalue.static intfloatToRawIntBits(float value)Returns an integer corresponding to the bits of the given IEEE 754 single precision floatvalue.floatfloatValue()Gets the primitive value of this float.inthashCode()Returns an integer hash code for this object.static floatintBitsToFloat(int bits)Returns the IEEE 754 single precision float corresponding to the givenbits.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.static StringtoHexString(float f)Converts the specified float into its hexadecimal string representation.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)Parses the specified string as a float value.
-
Field Details
-
MAX_VALUE
public static final float MAX_VALUEConstant for the maximumfloatvalue, (2 - 2-23) * 2127.- See Also:
- Constant Field Values
-
MIN_VALUE
public static final float MIN_VALUEConstant for the minimumfloatvalue, 2-149.- See Also:
- Constant Field Values
-
NaN
public static final float NaNConstant for the Not-a-Number (NaN) value of thefloattype.- See Also:
- Constant Field Values
-
POSITIVE_INFINITY
public static final float POSITIVE_INFINITYConstant for the positive infinity value of thefloattype.- See Also:
- Constant Field Values
-
NEGATIVE_INFINITY
public static final float NEGATIVE_INFINITYConstant for the negative infinity value of thefloattype.- See Also:
- Constant Field Values
-
MIN_NORMAL
public static final float MIN_NORMALConstant for the smallest positive normal value of thefloattype.- Since:
- 1.6
- See Also:
- Constant Field Values
-
MAX_EXPONENT
public static final int MAX_EXPONENTMaximum 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_EXPONENTMinimum 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
-
TYPE
TheClassobject that represents the primitive typefloat.- Since:
- 1.1
-
SIZE
public static final int SIZEConstant for the number of bits needed to represent afloatin two's complement form.- Since:
- 1.5
- See Also:
- Constant Field Values
-
-
Constructor Details
-
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
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 Details
-
compareTo
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
-
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. -
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.
-
equals
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()
-
floatToIntBits
public static int floatToIntBits(float value)Returns an integer corresponding to the bits of the given IEEE 754 single precision floatvalue. All Not-a-Number (NaN) values are converted to a single NaN representation (0x7fc00000) (compare tofloatToRawIntBits(float)). -
floatToRawIntBits
public static int floatToRawIntBits(float value)Returns an integer corresponding to the bits of the given IEEE 754 single precision floatvalue. Not-a-Number (NaN) values are preserved (compare tofloatToIntBits(float)). -
floatValue
public float floatValue()Gets the primitive value of this float.- Specified by:
floatValuein classNumber- Returns:
- this object's primitive value.
-
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)
-
intBitsToFloat
public static float intBitsToFloat(int bits)Returns the IEEE 754 single precision float corresponding to the givenbits. -
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. -
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.
-
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. -
parseFloat
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)
-
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.
-
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. The default implementation is equivalent to the following expression:getClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful
toStringmethod if you intend implementing your owntoStringmethod. -
toString
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
Parses the specified string as a float value.- Parameters:
string- the string representation of a float value.- Returns:
- a
Floatinstance containing the float value represented bystring. - Throws:
NumberFormatException- ifstringcan not be parsed as a float value.- See Also:
parseFloat(String)
-
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
-
valueOf
Returns aFloatinstance for the specified float value.- Parameters:
f- the float value to store in the instance.- Returns:
- a
Floatinstance containingf. - Since:
- 1.5
-
toHexString
Converts the specified float into its hexadecimal string representation.- Parameters:
f- the float to convert.- Returns:
- the hexadecimal string representation of
f. - Since:
- 1.5
-