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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/for_loop("`For Loop`") java/BasicSyntaxGroup -.-> java/if_else("`If...Else`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117644{{"`Java Float Compare Method`"}} java/classes_objects -.-> lab-117644{{"`Java Float Compare Method`"}} java/class_methods -.-> lab-117644{{"`Java Float Compare Method`"}} java/modifiers -.-> lab-117644{{"`Java Float Compare Method`"}} java/oop -.-> lab-117644{{"`Java Float Compare Method`"}} java/user_input -.-> lab-117644{{"`Java Float Compare Method`"}} java/wrapper_classes -.-> lab-117644{{"`Java Float Compare Method`"}} java/identifier -.-> lab-117644{{"`Java Float Compare Method`"}} java/arrays -.-> lab-117644{{"`Java Float Compare Method`"}} java/comments -.-> lab-117644{{"`Java Float Compare Method`"}} java/data_types -.-> lab-117644{{"`Java Float Compare Method`"}} java/for_loop -.-> lab-117644{{"`Java Float Compare Method`"}} java/if_else -.-> lab-117644{{"`Java Float Compare Method`"}} java/operators -.-> lab-117644{{"`Java Float Compare Method`"}} java/output -.-> lab-117644{{"`Java Float Compare Method`"}} java/strings -.-> lab-117644{{"`Java Float Compare Method`"}} java/variables -.-> lab-117644{{"`Java Float Compare Method`"}} java/system_methods -.-> lab-117644{{"`Java Float Compare Method`"}} end

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