Float Wrapper Class in Java

The Float class in Java is a wrapper class for the primitive data type float. It is part of the java.lang package and provides methods to work with float values, such as parsing, converting, and comparing them. The Float class also provides constants for representing positive and negative infinity, as well as NaN (Not-a-Number) values.

Key Features of Float Wrapper Class

  1. Immutability: Instances of the Float class are immutable.
  2. Constants: Provides constants like MAX_VALUE, MIN_VALUE, POSITIVE_INFINITY, NEGATIVE_INFINITY, and NaN.
  3. Static Methods: Methods for parsing strings to floats, comparing floats, and converting floats to strings.

Float Class Methods

Here are some commonly used methods in the Float class:

  1. byte byteValue()
  2. int compareTo(Float anotherFloat)
  3. static int compare(float f1, float f2)
  4. double doubleValue()
  5. boolean equals(Object obj)
  6. static float parseFloat(String s)
  7. float floatValue()
  8. int hashCode()
  9. int intValue()
  10. boolean isInfinite()
  11. static boolean isInfinite(float v)
  12. boolean isNaN()
  13. static boolean isNaN(float v)
  14. long longValue()
  15. static String toString(float f)
  16. String toString()
  17. static Float valueOf(String s)
  18. static Float valueOf(float f)

1. byte byteValue()

Returns the value of this Float object as a byte.

public class FloatExample {
    public static void main(String[] args) {
        Float f = 123.45f;
        byte value = f.byteValue();
        System.out.println(value);  // Output: 123
    }
}

Output:

123

2. int compareTo(Float anotherFloat)

Compares two Float objects numerically.

public class FloatExample {
    public static void main(String[] args) {
        Float f1 = 123.45f;
        Float f2 = 543.21f;
        System.out.println(f1.compareTo(f2));  // Output: -1
    }
}

Output:

-1

3. static int compare(float f1, float f2)

Compares two float values numerically.

public class FloatExample {
    public static void main(String[] args) {
        System.out.println(Float.compare(123.45f, 543.21f));  // Output: -1
    }
}

Output:

-1

4. double doubleValue()

Returns the value of this Float object as a double.

public class FloatExample {
    public static void main(String[] args) {
        Float f = 123.45f;
        double value = f.doubleValue();
        System.out.println(value);  // Output: 123.45
    }
}

Output:

123.44999694824219

5. boolean equals(Object obj)

Compares this object to the specified object.

public class FloatExample {
    public static void main(String[] args) {
        Float f1 = 123.45f;
        Float f2 = 123.45f;
        System.out.println(f1.equals(f2));  // Output: true
    }
}

Output:

true

6. static float parseFloat(String s)

Parses the string argument as a float.

public class FloatExample {
    public static void main(String[] args) {
        float f = Float.parseFloat("123.45");
        System.out.println(f);  // Output: 123.45
    }
}

Output:

123.45

7. float floatValue()

Returns the value of this Float object as a float.

public class FloatExample {
    public static void main(String[] args) {
        Float f = 123.45f;
        float value = f.floatValue();
        System.out.println(value);  // Output: 123.45
    }
}

Output:

123.45

8. int hashCode()

Returns a hash code for this Float object.

public class FloatExample {
    public static void main(String[] args) {
        Float f = 123.45f;
        System.out.println(f.hashCode());  // Output: a unique hash code
    }
}

Output:

1123477094

9. int intValue()

Returns the value of this Float object as an int.

public class FloatExample {
    public static void main(String[] args) {
        Float f = 123.45f;
        int value = f.intValue();
        System.out.println(value);  // Output: 123
    }
}

Output:

123

10. boolean isInfinite()

Checks if the value of this Float object is infinitely large in magnitude.

public class FloatExample {
    public static void main(String[] args) {
        Float f = Float.POSITIVE_INFINITY;
        System.out.println(f.isInfinite());  // Output: true
    }
}

Output:

true

11. static boolean isInfinite(float v)

Checks if the specified float value is infinitely large in magnitude.

public class FloatExample {
    public static void main(String[] args) {
        System.out.println(Float.isInfinite(Float.POSITIVE_INFINITY));  // Output: true
    }
}

Output:

true

12. boolean isNaN()

Checks if the value of this Float object is a Not-a-Number (NaN) value.

public class FloatExample {
    public static void main(String[] args) {
        Float f = Float.NaN;
        System.out.println(f.isNaN());  // Output: true
    }
}

Output:

true

13. static boolean isNaN(float v)

Checks if the specified float value is a Not-a-Number (NaN) value.

public class FloatExample {
    public static void main(String[] args) {
        System.out.println(Float.isNaN(Float.NaN));  // Output: true
    }
}

Output:

true

14. long longValue()

Returns the value of this Float object as a long.

public class FloatExample {
    public static void main(String[] args) {
        Float f = 123.45f;
        long value = f.longValue();
        System.out.println(value);  // Output: 123
    }
}

Output:

123

15. static String toString(float f)

Returns a String object representing the specified float.

public class FloatExample {
    public static void main(String[] args) {
        System.out.println(Float.toString(123.45f));  // Output: 123.45
    }
}

Output:

123.45

16. String toString()

Returns a String object representing this Float's value.

public class FloatExample {
    public static void main(String[] args) {
        Float f = 123.45f;
        System.out.println(f.toString());  // Output: 123.45
    }
}

Output:

123.45

17. static Float valueOf(String s)

Returns a Float object holding the value given by the specified String.

public class FloatExample {
    public static void main(String[] args) {
        Float f = Float.valueOf("123.45");
        System.out.println(f);  // Output: 123.45
    }
}

Output:

123.45

18. static Float valueOf(float f)

Returns a Float instance representing the specified float value.

public class FloatExample {
    public static void main(String[] args) {
        Float f = Float.valueOf(123.45f);
        System.out.println(f);  // Output: 123.45
    }
}

Output:

123.45

Conclusion

The Float class in Java provides a wide range of methods for manipulating and converting float values. Understanding and utilizing these methods can greatly enhance your ability to handle floating-point operations in your applications.

Comments