The method compares the two specified float values. The sign of the integer value returned is the same as that of the integer that would be returned by the call −

new Float(f1).compareTo(new Float(f2))


Following is the declaration for method

public static int compare(float f1, float f2)


  • f1 − This is the first float to compare.

  • f2 − This is the second float to compare.

Return Value

This method 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.




The following example shows the usage of method.

package com.tutorialspoint;

import java.lang.*;

public class FloatDemo {

   public static void main(String[] args) {

      // compares the two specified float values
      float f1 = 22.30f;
      float f2 = 88.67f;
      int retval =, f2);
      if(retval > 0) {
         System.out.println("f1 is greater than f2");
      } else if(retval < 0) {
         System.out.println("f1 is less than f2");
      } else {
         System.out.println("f1 is equal to f2");

Let us compile and run the above program, this will produce the following result −

f1 is less than f2