Class Float in Java

Float Class wraps a value of type float in an immutable Object. An Object of type Float contains a single field whose type is a float.

The Float class provides an object wrapper for a float value. This is useful when you need to treat a float value as an object. For example, there are a number of utility methods that take a reference to an Object as one of their arguments. You cannot specify a float value for one of these arguments, but you can provide a reference to a Float object that encapsulates the float value.

valueOf() converts a string to a Float, floatValue() returns the primitive float value of a Float object, and there are methods for returning a Float value as a variety of other primitive types.

In Java, float values are represented using the IEEE 754 format. The Float class provides constants for the three special values that are mandated by this format. This class also provides some useful constants and static methods for testing float values. MIN_VALUE and MAX_VALUE are the smallest (closest to zero) and largest representable double values.

isInfinite() in a class method and instance method forms test whether a float or a Float has an infinite value. Similarly, isNaN() tests whether a float or Float is not-a-number–this is a comparison that cannot be done directly because the NaN constant never tests equal to any other value, including itself. floatToIntBits() and intBitsToFloat() allow you to manipulate the bit representation of a float directly.

The structure of the class Float is given as:


public final class java.lang.Float extends java.lang.Number{
//Member Elements
public final static float MAX_VALUE; //MAX_VALUE=3.400282346638528860 e to the power 38 is the largest positive finite value of type float.
public final static float MIN_VALUE; //MIN_VALUE=1.40129846432481707 e to the power -45 is the smallest positive finite value of type float.
public final static float NaN;//NaN=0.0f/0.0f is a non number value of type float
public final static double NEGATIVE_INFINITY;//produced when a negative number is divided by 0 like -10.0f/0.0f
public final static double POSITIVE_INFINITY;//produced when a positive number is divided by 0 like 10.0f/0.0f
public static final Class TYPE;
//constructors
public Float(double value);//constructs a newly allocated Float Object that represents the argument converted to type float
public Float(float value);//constructs a newly allocated Float Object that represents the primitive type float argument.
public Float(String str);//Constructs a newly allocated Float object that represents the floating point value of type float 
//represented by String str.The string is converted to a double value by valueOf() method.
//This method may throw NumberFormatException if the string does not contain a parsable number.
//Methods:
public double doubleValue();
public boolean equals(Object obj);
public boolean isNaN();
public static native int floatToIntBits(float value);
public static Boolean isInfinite(float value);
public static Boolean isNaN(float value);
public static String toString(float value);
public static Float valueOf(String str)throws NumberFormatException;
public float floatValue();
public byte byteValue();
public int hashCode();
public static native float intBitsToFloat(int bits);
public int intValue();
public boolean isInfinite();
public short shortValue();
public long longValue();
public String toString();
}

The details of the class structure are given as follows:

public static final float MIN_VALUE;

public static final float MIN_VALUE is the smallest value that can be represented by a float.

public static final float MAX_VALUE;

public static final float MAX_VALUE is the largest value that can be represented by a float.

public static final float NaN;

public static final float NaN represents the value NaN, a special value produced by float operations such as division of zero by zero.

When NaN is one of the operands, most arithmetic operations return NaN as the result. Most comparison operators (<, <=, ==, >=, >) return false when one of their arguments is NaN. The exception is !=, which returns true when one of its arguments is NaN.

public static final float NEGATIVE_INFINITY;

public static final float NEGATIVE_INFINITY represents the value negative infinity, which is produced when a float operation underflows or a negative float value is divided by zero. Negative infinity is by definition less than any other float value.

public static final float POSITIVE_INFINITY;

public static final float POSITIVE_INFINITY represents the value positive infinity, which is produced when a float operation overflows or a positive float value is divided by zero. Positive infinity is by definition greater than any other float value.

public final static Class TYPE;

public final static Class TYPE is the Class object that represents the type float. It is always true that Float.TYPE == float.class.

public Float(double value);

public Float(double value) constructor creates a Float object with the specified double value. The value is rounded to float precision.

Parameter
value – The double value to be encapsulated by this object.

public Float(float value);

public Float(float value) constructor Constructs a Float object with the value specified by the given string. The string must contain a sequence of characters that forms a legal float literal.
Parameter
value -The float value to be encapsulated by this object.

public Float(String s);

public Float(String s) constructor constructs a Float object with the value specified by the given string. The string must contain a sequence of characters that forms a legal float literal.
Parameter
s – The string to be made into a Float object.

public static native int floatToIntBits(float value);

public static native int floatToIntBits(float value) method returns the int value that contains the same sequence of bits as the representation of the given float value. The meaning of the bits in the result is defined by the IEEE 754 floating-point format: bit 31 is the sign bit, bits 30-23 are the exponent, and bits 22-0 are the mantissa.

An argument of POSITIVE_INFINITY produces the result 0x7f800000, an argument of NEGATIVE_INFINITY produces the result 0xff800000, and an argument of NaN produces the result 0x7fc00000.

The value returned by this method can be converted back to the original float value by the
intBitsToFloat() method.

This method returns the int value that contains the same sequence of bits as the representation of the given float value.
Parameter
value -The float value to be converted.

public static native float intBitsToFloat(int bits);

public static native float intBitsToFloat(int bits) method returns the float value whose representation is the same as the bits in the given int value. The meaning of the bits in the int value is defined by the IEEE 754 floating-point format: bit 31 is the sign bit, bits 30-23 are the exponent, and bits 22-0 are the mantissa.

The argument 0x7f800000 produces the result POSITIVE_INFINITY, and the argument
0xff800000 produces the result NEGATIVE_INFINITY. Arguments in the ranges
0x7f800001 through 0x7f8fffff and 0xff800001 through 0xff8fffffL all produce the result NaN.

Except for NaN values not normally used by Java, this method is the inverse of the
floatToIntBits() method.

This method returns the float value whose representation is the same as the bits in the given int value.

Parameter
bits – The int value to be converted.

public static boolean isInfinite(float v);

public static boolean isInfinite(float v) method determines whether or not the specified value is an infinity value.

This method returns true if the specified value is equal to POSITIVE_INFINITY or NEGATIVE_INFINITY;otherwise false.

Parameter
v – The float value to be tested.

public static boolean isNaN(float v);

public static boolean isNaN(float v) method determines whether or not the specified value is NaN.
This method returns true if the specified value is equal to NaN; otherwise false.
Parameter
v – The float value to be tested.

public static String toString(float f);

public static String toString(float f) method returns a String object that contains a representation of the given float value. The values NaN, NEGATIVE_INFINITY, POSITIVE_INFINITY, -0.0, and +0.0 are represented by the strings “NaN”, “–Infinity”, “Infinity”, “–0.0”, and “0.0”, respectively.

For other values, the exact string representation depends on the value being converted. If the absolute value of f is greater than or equal to 10^-3 or less than or equal to 10^7, it is converted to a string with an optional minus sign (if the value is negative) followed by up to eight digits before the decimal point, a decimal point, and the necessary number of digits after the decimal point (but no trailing zero if there is more than one significant digit). There is always a minimum of one digit after the decimal point.

Otherwise, the value is converted to a string with an optional minus sign (if the value is negative), followed by a single digit, a decimal point, the necessary number of digits after the decimal point (but no trailing zero if there is more than one significant digit), and the letter E followed by a plus or a minus sign and a base 10 exponent of at least one digit. Again, there is always a minimum of one digit after the decimal point.

This method returns a string representation of the given value.
Parameter
v – The float value to be converted.

public static Float valueOf(String s);

public static Float valueOf(String s) method Constructs a Float object with the value specified by the given string. The string must contain a sequence of characters that forms a legal float literal. This method ignores leading and trailing whitespace in the string.

This method returns the Float object constructed from the string.
Parameter
s – The string to be made into a Float object.

public byte byteValue();

public byte byteValue() method returns the truncated value of this object as a byte. More specifically, if the value of the object is NaN, the method returns 0. If the value is POSITIVE_INFINITY or any other value that is too large to be represented by a byte, the method returns Byte.MAX_VALUE.

If the value is NEGATIVE_INFINITY or any other value that is too small to be represented by a byte, the method returns Byte.MIN_VALUE. Otherwise, the value is rounded toward zero and returned.
This method returns the value of this object as a byte.

public double doubleValue();

public double doubleValue() method returns the value of this object as a double.
This method returns the value of this object as a double.

public boolean equals(Object obj);

public boolean equals(Object obj) method returns true if obj is an instance of Float and it contains the same value as the object this method is associated with. More specifically, the method returns true if the floatToIntBits() method returns the same result for the values of both objects.

This method produces a different result than the == operator when both values are NaN. In this case, the == operator produces false, while this method returns true. By the same token, the method also produces a different result when the two values are +0.0 and -0.0. In this case, the == operator produces true, while this method returns false.

This method returns true if the objects are equal; false if they are not.
Parameter
obj -The object to be compared with this object.

public float floatValue();

public float floatValue() method returns the value of this object as a float.
This method returns the value of this object as a float.

public int hashCode();

public int hashCode() method returns a hashcode computed from the value of this object. More specifically, if f is the value of the object, this method returns Float.floatToIntBits(f).
This method returns a hashcode based on the float value of the object.

public int intValue();

public int intValue() method returns the truncated value of this object as an int. More specifically, if the value of the object is NaN, the method returns 0. If the value is POSITIVE_INFINITY or any other value that is too large to be represented by an int, the method returns Integer.MAX_VALUE.

If the value is NEGATIVE_INFINITY or any other value that is too small to be represented by
an int, the method returns Integer.MIN_VALUE. Otherwise, the value is rounded toward zero
and returned.
This method returns the value of this object as an int.

public boolean isInfinite();

public boolean isInfinite() method determines whether or not the value of this object is an infinity value.
This method returns true if the value of this object is equal to POSITIVE_INFINITY or NEGATIVE_INFINITY; otherwise false.

public boolean isNaN();

public boolean isNaN() method determines whether or not the value of this object is NaN.
This method returns true if the value of this object is equal to NaN; otherwise false.

public long longValue();

public long longValue() method returns the truncated value of this object as a long. More specifically, if the value of the object is NaN, the method returns 0. If the value is POSITIVE_INFINITY or any other value that is too large to be represented by a long, the method returns Long.MAX_VALUE.

If the value is NEGATIVE_INFINITY or any other value that is too small to be represented by a long, the method returns Long.MIN_VALUE. Otherwise, the value is rounded toward zero and returned.

This method returns the value of this object as a long.

public short shortValue();

public short shortValue() method returns the truncated value of this object as a short. More specifically, if the value of the object is NaN, the method returns 0. If the value is POSITIVE_INFINITY or any other value that is too large to be represented by a short, the method returns Short.MAX_VALUE.

If the value is NEGATIVE_INFINITY or any other value that is too small to be represented by
a short, the method returns Short.MIN_VALUE. Otherwise, the value is rounded toward zero
and returned.

This method returns the value of this object as a short.

public String toString();

public String toString() method returns a String object that contains a representation of the value of this object.

The values NaN, NEGATIVE_INFINITY, POSITIVE_INFINITY, -0.0, and +0.0 are represented by the strings “NaN”, “–Infinity”, “Infinity”, “–0.0”, and “0.0”,respectively.

For other values, the exact string representation depends on the value being converted. If the absolute value of this object is greater than or equal to 10^-3 or less than or equal to 10^7, it is converted to a string with an optional minus sign (if the value is negative) followed by up to eight digits before the decimal point, a decimal point, and the necessary number of digits after the decimal point (but no trailing zero if there is more than one significant digit). There is always a minimum of one digit after the decimal point.

Otherwise, the value is converted to a string with an optional minus sign (if the value is negative), followed by a single digit, a decimal point, the necessary number of digits after the decimal point (but no trailing zero if there is more than one significant digit), and the letter E followed by a plus or a minus sign and a base 10 exponent of at least one digit. Again, there is always a minimum of one digit after the decimal point.

This method returns a string representation of the value of this object.
Apart from these Float class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on [email protected]

Write A Comment