java.lang.Float compare()

Description

The compare() method of Float class is used simply to compare two float values on which one is higher. The compare(float f1, float f2) returns the int equivalent of comparing the method argument f1 and f2. All the wrapper class has their own implementation of the compare method which gave us a lot of flexibility in comparing two numbers regardless of what data type is. It might be argued to use our own comparator instead, but designing a good comparator requires a lot of time and deligence but this method is more robust and will not rely on the inherent behavior of the compare method of the wrapper classes. Both ways has their pros and cons, it’s up to the java programmer to decide which suits him best.

Important Notes:

  • The method compare(float f1, float f2) will yield the same result as new Float(f1).compareTo(new Float(f2))

Method Syntax

public static int compare(float f1, float f2)

Method Argument

Data Type Parameter Description
float f1 the first float to compare
float f2 the second float to compare

Method Returns

The compare() method of Float class returns the value 0 if f1 is numerically equal to f2; a value less than 0 if f1 is numerically less than f2; and a value greater than 0 if f1 is numerically greater than f2.

Discussion

 To differentiate two numbers, usually our approach is to make use of if else condition, and base on the results of the evaluation we determine the which one of the two numbers are greater or equality. The compare method were devised as such that we can test the equality or compare two numbers without going the trouble of formulating the conditional statements. Well we might still be using the if condition however the conditional statements are more consistent now.

Java Float comapre(float f1, float f2) Example

Below is a simple java example on the usage of compare() method of Float class.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

import static java.lang.System.*;

/*
 * This example source code demonstrates the use of 
 * compare(float f1, float f2) method of Float class.
 */

public class FloatCompareExample {

	public static void main(String[] args) {

		// Ask user input for first number
		System.out.print("Enter First Number:");
		// declare the scanner object
		Scanner scan = new Scanner(System.in);
		// use scanner to get value from user console
		Float f1 = scan.nextFloat();
		// Ask user input for second number
		System.out.print("Enter Second Number:");
		Float f2 = scan.nextFloat();
		// close the scanner object
		scan.close();
		int result = Float.compare(f1, f2);
		System.out.println("Result of comparing f1 and f2:" + result);

		if (result == 1) {
			out.println("f1 is greater than f2");
		} else if (result == 0) {
			out.println("f1 is equal to f2");
		} else {
			out.println("f1 is less than f2");
		}

	}

}

Basically on the above example, we just ask for two numbers on the console and then we use the scanner object to get the float inputs. We then determine which of the two numbers were higher using the compare method.

Sample Output

Below is the sample output when you run the above example.