Updates
  • Starting New Weekday Batch for Full Stack Java Development on 15 September 2025 @ 02:00 PM to 04:00 PM
  • Starting New Weekday Batch for MERN Stack Development on 29 September 2025 @ 04:00 PM to 06:00 PM
Join Course

Java Full Stack Developer Training Center in Noida

java.lang.Float

Float is a wrapper class available in the java.lang package. This class is primarily used to encapsulate float literals as objects.

Important points of java.lang.Float class:

1. We cannot perform any changes on an existing Float class object.
2. The java.lang.Float class is a final class, so no other class can extend it, and we cannot override any method of the java.lang.Float class

            
public class JTC {
   public static void main(String[] args) {
      Float f1 = newFloat(10.67 f);
      Float f2 = newFloat(23.45 f);
      Float f3 = f1 + f2;
      System.out.println("f1 == f3 :- " + (f1 == f3)); // false
      System.out.println("f2 == f3 :- " + (f2 == f3)); // false
   }
}
    f1 == f3 :- false
    f2 == f3 :- false
            

As we can see in this example, we are creating two different objects of the java.lang.Float class and storing their results of the addition arithmetic operation into Float f3. As we know, a Float class object is immutable in nature. That is the reason when we compare F1 and F2 or F2 and F3, we are getting false. This shows that when we are adding F1 and F2, internally, a new object of the Float class gets created instead of changes happening in the existing object of the Float class.

• java.lang.Float class: -

public final class java.lang.Float extends java.lang.Number implements java.lang.Comparable<java.lang.Float> {
public static final float POSITIVE_INFINITY;
public static final float NEGATIVE_INFINITY;
public static final float NaN;
public static final float MAX_VALUE;
public static final float MIN_NORMAL;
public static final float MIN_VALUE;
public static final int MAX_EXPONENT;
public static final int MIN_EXPONENT;
public static final int SIZE;
public static final int BYTES;
public static final java.lang.Class<java.lang.Float> TYPE;
public static java.lang.String toString(float);
public static java.lang.String toHexString(float);
public static java.lang.Float valueOf(java.lang.String) throws java.lang.NumberFormatException;
public static java.lang.Float valueOf(float);
public static float parseFloat(java.lang.String) throws java.lang.NumberFormatException;
public static boolean isNaN(float);
public static boolean isInfinite(float);
public static boolean isFinite(float);
public java.lang.Float(float);
public java.lang.Float(double);
public java.lang.Float(java.lang.String) throws java.lang.NumberFormatException;
public boolean isNaN();
public boolean isInfinite();
public java.lang.String toString();
public byte byteValue();
public short shortValue();
public int intValue();
public long longValue();
public float floatValue();
public double doubleValue();
public int hashCode();
public static int hashCode(float);
public boolean equals(java.lang.Object);
public static int floatToIntBits(float);
public static native int floatToRawIntBits(float);
public static native float intBitsToFloat(int);
public int compareTo(java.lang.Float);
public static int compare(float, float);
public static float sum(float, float);
public static float max(float, float);
public static float min(float, float);
public int compareTo(java.lang.Object);
static {};
}

Important Constructor of java.lang.Float class:

1. public java.lang.Float(double): It helps us to create an object of the java.lang.Float class, which represents the double type argument as a float.

2. public java.lang.Float(float): It constructs a new Float class object that wraps the specified primitive type float value.

3. public java.lang.Float(String): It constructs a newly allocated Float object that represents the floating-point value of type float represented by the string. The string is converted to a float value as if by the valueOf method. When the specified String is not in the Number Format, we get a NumberFormatException at the time of execution of the program. If null is passed as an argument, a NullPointerException is thrown.

            
public class JTC {
   public static void main(String[] args) {
      // Creating Object of java.lang.Float class using double type primitive value.
      Float f1 = newFloat(1023.345);
      System.out.println("f1 :- " + f1);
      // Creating Object of java.lang.Float class using float type primitive value.
      Float f2 = newFloat(12.34 f);
      System.out.println("f2 :- " + f2);
      // Creating Object of java.lang.Float class using String type value.
      Float f3 = newFloat("123.89");
      Float f4 = newFloat("-786.87f");
      Float f5 = newFloat("12");
      // Float f6 = new Float(null); ---> NullPointerException
      // Float f7 = new Float("abc"); --> NumberFormatException
      System.out.println("f3 :- " + f3);
      System.out.println("f4 :- " + f4);
      System.out.println("f5 :- " + f5);
   }
}
    f1 :- 1023.345
    f2 :- 12.34
    f3 :- 123.89
    f4 :- -786.87
    f5 :- 12.0
            

Important Variables of java.lang.Float class:

1. public static final float POSITIVE_INFINITY: A constant holding the positive infinity of type float. It is equal to the value returned by Float.intBitsToFloat(0x7f800000).

public static native float intBitsToFloat(int):

It returns the float value corresponding to a given bit representation. The argument is considered to be a representation of a floating-point value according to the IEEE 754 floating-point "single format" bit layout.
If the argument is 0x7f800000, the result is positive infinity.
If the argument is 0xff800000, the result is negative infinity.
If the argument is any value in the range 0x7f800001 through 0x7fffffff or in the range 0xff800001 through 0xffffffff, the result is a NaN. No IEEE 754 floating-point operation provided by Java can distinguish between two NaN values of the same type with different bit patterns. Distinct values of NaN are only distinguishable by use of the Float.floatToRawIntBits method.
In all other cases, let s, e and m be three values that can be computed from the argument:

            
    int s = ((bits >> 31) == 0) ? 1 : -1; 
int e = ((bits >> 23) & 0xff);
int m = (e == 0) ? (bits & 0x7fffff) << 1 : (bits & 0x7fffff) | 0x800000;

Then the floating-point result equals the value of the mathematical expression s•m•2e-150.
Note that this method may not be able to return a float NaN with the exactly same bit pattern as the int argument. IEEE 754 distinguishes between two kinds of NaNs: quiet NaNs and signaling NaNs. The differences between the two kinds of NaN are generally not visible in Java. Arithmetic operations on signaling NaNs turn them into quiet NaNs with a different, but often similar, bit pattern. However, on some processors, merely copying a signaling NaN also performs that conversion. In particular, copying a signaling NaN to return it to the calling method may perform this conversion. Therefore, intBitsToFloat may not be able to return a float with a signaling NaN bit pattern. Consequently, for some int values, floatToRawIntBits(intBitsToFloat(start)) may not equal start. Moreover, the specific bit patterns that represent signaling NaNs are platform-dependent, although all NaN bit patterns, whether quiet or signaling, must be in the NaN range identified above.
It occurs when we perform an arithmetic operation with float-type operands, and the result will be + Infinity according to mathematics.

2. public static final float NEGATIVE_INFINITY: A constant holding the negative infinity of type float. It is equal to the value returned by Float.intBitsToFloat(0xff800000).
It occurs when we perform an arithmetic operation with float-type operands, and the result becomes negative infinity according to mathematics.

3. public static final float NaN: A constant holding a Not-a-Number (NaN) value of type float. It is equivalent to the value returned by Float.intBitsToFloat(0x7fc00000).
It occurs when we perform an arithmetic operation with float-type operands, and the result becomes undefined according to mathematics.

4. public static final float MAX_VALUE: It holds the positive finite value of 3.4028235E38.

5. public static final float MIN_VALUE: A constant holding the smallest positive nonzero value of type float, which is 1.4E-45.

            
public class JTC {
   public static void main(String[] args) {
      System.out.println("Float Max Value :- " + Float.MAX_VALUE);
      System.out.println("Float Min Value :- " + Float.MIN_VALUE);
      System.out.println("Positive Infinity :- " + Float.POSITIVE_INFINITY);
      System.out.println("Negative Infinity :- " + Float.NEGATIVE_INFINITY);
      System.out.println("NaN :- " + Float.NaN);
      System.out.println(12.34 f / 0.0 f);
      System.out.println(-12.34 / 0.0);
      System.out.println(0.0 / 0.0);
   }
}
    Float Max Value :- 3.4028235E38
    Float Min Value :- 1.4E-45
    Positive Infinity :- Infinity
    Negative Infinity :- -Infinity
    NaN :- NaN
    Infinity
    -Infinity
    NaN
            

Important Method of java.lang.Float:

1. public static java.lang.String toHexString(float): It converts the specified float value into a hexadecimal and returns it as a string.

2. public static float parseFloat(java.lang.String) throws java.lang.NumberFormatException; This method helps parse a string into a float, returning the parsed float value. It throws a NullPointerException when a null argument is passed and a NumberFormatException when the provided string is not in a parseable float format.

3. public static java.lang.Float valueOf(java.lang.String) throws java.lang.NumberFormatException: Returns a Float object holding the float value represented by the argument. If the argument is null, a NullPointerException is thrown. Leading and trailing whitespace characters in the argument are ignored and removed.

4. public static java.lang.Float valueOf(float): It creates a new object of the java.lang.Float class that represents the specified float value and returns the newly created object.

5. public byte byteValue(): It returns the current float-type value of the working object as a byte-type value after typecasting.

6. public short shortValue(): It returns the current float-type value of the working object as a short-type value after typecasting.

7. public int intValue(): It returns the current float-type value of the working object as an int-type value after typecasting.

8. public long longValue(): It returns the current float-type value of the working object as a long-type value after typecasting.

9. public float floatValue(): It returns the float value represented by the specified current working Float object.

10. public double doubleValue(): It returns the current float-type value of the working object as a double-type value using implicit type-casting.

11. public int compareTo(java.lang.Float): We use this method to compare two different objects of the java.lang.Float type. If the current working object's float value is greater than the specified object's float value, it returns 1. If the current working object's float value is less than the specified object's float value, it returns -1. If both float-type objects have the same value, it returns 0.

12. public boolean equals(java.lang.Object): It compares two objects of the java.lang.Float type. If the current working object and the specified object have the same float value, it returns true; otherwise, it returns false.

            
public class JTC {
   public static void main(String[] args) {
      String hex = Float.toHexString(12.00 f);
      System.out.println("hex :- " + hex);
      floatf1 = Float.parseFloat("12.34");
      // Float.parseFloat(null); ---> NullPointerException
      // Float.parseFloat("true"); ---> NumbeeFormatException
      System.out.println("f1 :- " + f1);
      Float f2 = Float.valueOf("12.34");
      System.out.println("f2 :- " + f2);
      Float f3 = Float.valueOf("\t12.34\n");
      System.out.println("f3 :- " + f3);
      // Float.valueOf(null); ---> NullPointerException
      Float f4 = Float.valueOf(12.34 f);
      System.out.println("f4 :- " + f4);
      Float float1 = newFloat(12.34);
      byteb1 = float1.byteValue();
      System.out.println("b1 :- " + b1);
      shorts1 = float1.shortValue();
      System.out.println("s1 :- " + s1);
      inti1 = float1.intValue();
      System.out.println("i1 :- " + i1);
      longl1 = float1.longValue();
      System.out.println("l1 :- " + l1);
      floatf5 = float1.floatValue();
      System.out.println("f5 :- " + f5);
      doubled1 = float1.doubleValue();
      System.out.println("d1 :- " + d1);
      Float float2 = newFloat(23.45);
      System.out.println("float1.compareTo(float2) :- " + float1.compareTo(float2));
      System.out.println("float2.compareTo(float1) :- " + float2.compareTo(float1));
      Float float3 = newFloat(12.34);
      System.out.println("float1.compareTo(float3) :- " + float1.compareTo(float3));
      System.out.println("float1.equals(float2) :- " + float1.equals(float2));
      System.out.println("float1.equals(float3) :- " + float1.equals(float3));
   }
}
    hex :- 0x1.8p3
    f1 :- 12.34
    f2 :- 12.34
    f3 :- 12.34
    f4 :- 12.34
    b1 :- 12
    s1 :- 12
    i1 :- 12
    l1 :- 12
    f5 :- 12.34
    d1 :- 12.34000015258789
    float1.compareTo(float2) :- -1
    float2.compareTo(float1) :- 1
    float1.compareTo(float3) :- 0
    float1.equals(float2) :- false
    float1.equals(float3) :- true
            

In this example, we are using different important methods of the java.lang.Float class and trying to understand their implementation and restrictions.