Here we show how to compare a Float object with other Float object, Float with an Object, or two float primitive values with methods provided by java.lang.Float class.
Source Code
package com.beginner.examples;
public class CompareToFloatExample {
public static void main(String[] args) {
float f1 = 2.2f;
float f2 = 2.02f;
//Compare using the compare() method in the Float class
//1: f1 is greater than f2
//-1: f1 is less than f2
//0: f1 equals f2
int result = Float.compare(f1, f2);
System.out.println(result);
Float float1 = new Float(22.1);
Float float2 = new Float(22.2);
//Use the compareTo() method in the Float object to compare
int result2 = float1.compareTo(float2);
System.out.println(result2);
}
}
Output:
1
-1