Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Float Class

The Float class in Java is a wrapper class for the primitive float data type. It is part of the java.lang package and provides various methods and constants to work with float values. In this tutorial, we'll discuss the key features and methods of the Float class in Java.

  • Creating Float objects

You can create a Float object by providing a float value or a String representation of a float value to its constructor:

Float f1 = new Float(3.14f);
Float f2 = new Float("3.14");

Alternatively, you can use the valueOf() method to create a Float object:

Float f3 = Float.valueOf(3.14f);
Float f4 = Float.valueOf("3.14");
  • Constants

The Float class provides the following constants:

  • POSITIVE_INFINITY: Represents positive infinity.
  • NEGATIVE_INFINITY: Represents negative infinity.
  • NaN (Not a Number): Represents a value that is not a number.
  • MAX_VALUE: Represents the maximum positive finite representable value.
  • MIN_VALUE: Represents the minimum positive non-zero representable value.
  • MIN_NORMAL: Represents the minimum positive normal value.
  • MAX_EXPONENT: Represents the maximum positive exponent value.
  • MIN_EXPONENT: Represents the minimum negative exponent value.
  • Converting between float and String

To convert a float value to a String, you can use the toString() method:

float value = 3.14f;
String stringValue = Float.toString(value);

To parse a String to a float value, use the parseFloat() method:

String stringValue = "3.14";
float value = Float.parseFloat(stringValue);
  • Comparing Float objects

To compare two Float objects, you can use the compareTo() method:

Float f1 = new Float(3.14f);
Float f2 = new Float(1.23f);
int result = f1.compareTo(f2);

A negative value indicates that f1 is less than f2, a positive value indicates that f1 is greater than f2, and zero indicates that f1 and f2 are equal.

  • Checking for infinity and NaN

You can use the following methods to check for infinity and NaN values:

  • isInfinite(): Returns true if the value is positive infinity or negative infinity.
  • isNaN(): Returns true if the value is not a number (NaN).
  • isFinite(): Returns true if the value is a finite float value (not infinity or NaN).

Example:

Float f1 = new Float(3.14f);
Float f2 = new Float(Float.POSITIVE_INFINITY);
Float f3 = new Float(Float.NaN);

System.out.println("f1 is finite: " + Float.isFinite(f1));
System.out.println("f2 is infinite: " + f2.isInfinite());
System.out.println("f3 is NaN: " + Float.isNaN(f3));

In conclusion, the Float class in Java provides a wrapper for the primitive float data type, along with various methods and constants for working with float values. The Float class allows you to create Float objects, convert between float and String representations, compare Float objects, and check for special values such as infinity and NaN.

  1. Converting float to string in Java

    You can convert a float to a String using String.valueOf() or Float.toString().

    float floatValue = 3.14f;
    String stringValue = String.valueOf(floatValue);
    
  2. Java Float class methods and examples

    The Float class provides methods for working with float values.

    float floatValue = 3.14f;
    
    // Convert float to String
    String stringValue = Float.toString(floatValue);
    
    // Get the float value as int
    int intValue = Float.floatToIntBits(floatValue);
    
  3. Parsing strings to float in Java

    Use Float.parseFloat() or Float.valueOf() to parse a String to a float.

    String strValue = "3.14";
    float floatValue = Float.parseFloat(strValue);
    
  4. Handling NaN and Infinity in Java Float

    Floating-point numbers can represent special values like NaN (Not a Number) and Infinity.

    float nanValue = Float.NaN;
    float infinityValue = Float.POSITIVE_INFINITY;
    
  5. Rounding and precision with Java Float

    You can round and control precision using Math.round() or DecimalFormat.

    float floatValue = 3.14159f;
    
    // Round to 2 decimal places
    float roundedValue = Math.round(floatValue * 100.0) / 100.0;
    
    // Using DecimalFormat for precision
    DecimalFormat df = new DecimalFormat("#.##");
    String formattedValue = df.format(floatValue);
    
  6. Formatting float values in Java

    Use String.format() or DecimalFormat to format float values.

    float floatValue = 3.14159f;
    
    // Using String.format
    String formattedValue = String.format("%.2f", floatValue);
    
    // Using DecimalFormat
    DecimalFormat df = new DecimalFormat("#.##");
    String formattedValue = df.format(floatValue);
    
  7. Comparing float values in Java with Float.compare()

    Use Float.compare() to compare float values, considering NaN and ordering.

    float floatValue1 = 3.14f;
    float floatValue2 = 2.71f;
    
    int result = Float.compare(floatValue1, floatValue2);
    
    if (result > 0) {
        System.out.println("floatValue1 is greater");
    } else if (result < 0) {
        System.out.println("floatValue2 is greater");
    } else {
        System.out.println("floatValue1 and floatValue2 are equal");
    }