Java Float Compare Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will explore the usage of the compare() method of the Float class in Java. This method is used to compare two float values and returns an integer value that indicates which value is greater.

Creating a Java file

Create a new Java file named FloatCompareMethod.java in the ~/project/ directory using the following command in the terminal:

$ cd ~/project/
$ touch FloatCompareMethod.java

Define the main() Method

The main() method is the entry point for executing Java programs. Add the following code to the FloatCompareMethod.java file:

public class FloatCompareMethod {
    public static void main(String[] args) {
        // Write code here
    }
}

Implement Float.compare() Method

In the main() method, we will implement the compare() method. The compare() method compares two float values and returns an integer value that indicates which value is greater. Add the following code to the main() method:

float floatValue1 = 20.5f;
float floatValue2 = 10.8f;
int result = Float.compare(floatValue1, floatValue2);
if (result > 0) {
  System.out.println(floatValue1 + " is greater than " + floatValue2);
} else if (result < 0) {
  System.out.println(floatValue1 + " is less than " + floatValue2);
} else {
  System.out.println(floatValue1 + " is equal to " + floatValue2);
}

Compile the Java File

To compile the FloatCompareMethod.java file, open the terminal and execute the following command:

$ javac FloatCompareMethod.java

Run the Java Program

To run the compiled Java program, execute the following command in the terminal:

$ java FloatCompareMethod

You should see the output printed on the terminal:

20.5 is greater than 10.8

Implement Float.compare() Method with User Input

In this step, we will take input from the user to compare two float values. Add the following code to the main() method:

Scanner scanner = new Scanner(System.in);
System.out.print("Enter first float value: ");
float floatValue1 = scanner.nextFloat();
System.out.print("Enter second float value: ");
float floatValue2 = scanner.nextFloat();
int result = Float.compare(floatValue1, floatValue2);
if (result > 0) {
  System.out.println(floatValue1 + " is greater than " + floatValue2);
} else if (result < 0) {
  System.out.println(floatValue1 + " is less than " + floatValue2);
} else {
  System.out.println(floatValue1 + " is equal to " + floatValue2);
}

Compile the Java File

To compile the FloatCompareMethod.java file, open the terminal and execute the following command:

$ javac FloatCompareMethod.java

Run the Java Program

To run the compiled Java program, execute the following command in the terminal:

$ java FloatCompareMethod

You should see the output like:

Enter first float value: 12.4
Enter second float value: 12.4
12.4 is equal to 12.4

Implement Float.compare() Method with an array

In this step, we will compare an array of float values with a given float value. Add the following code to the main() method:

float[] floatValues = {10.4f, 20.6f, 30.8f, 40.2f, 50.6f};
float givenFloatValue = 20.6f;
for (float floatValue : floatValues) {
  int result = Float.compare(givenFloatValue, floatValue);
  if (result > 0) {
    System.out.println(givenFloatValue + " is greater than " + floatValue);
  } else if (result < 0) {
    System.out.println(givenFloatValue + " is less than " + floatValue);
  } else {
    System.out.println(givenFloatValue + " is equal to " + floatValue);
  }
}

Compile and Run the Java Program

To compile and run the FloatCompareMethod.java program, execute the following commands in the terminal:

$ javac FloatCompareMethod.java
$ java FloatCompareMethod

You should see the output printed on the terminal:

20.6 is equal to 10.4
20.6 is equal to 20.6
20.6 is less than 30.8
20.6 is less than 40.2
20.6 is less than 50.6

Summary

In this lab, we learned how to use the compare() method of the Float class in Java to compare two float values numerically to find which one is greater than the other. We also implemented the compare() method with user input and an array. This method is useful for sorting or comparing floating-point values in Java.

Other Java Tutorials you may like