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
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.
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");
The Float class provides the following constants:
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);
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.
You can use the following methods to check for infinity and NaN values:
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.
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);
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);
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);
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;
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);
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);
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"); }