Java Float Min Method

JavaJavaBeginner
Practice Now

Introduction

In some cases, we need to compare two floating-point numbers and determine which one is smaller. The Java 'Float' class provides a method min() that returns the smaller value out of two floating-point numbers passed to it.


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/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/math("`Math`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/math_methods("`Math Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117678{{"`Java Float Min Method`"}} java/classes_objects -.-> lab-117678{{"`Java Float Min Method`"}} java/class_methods -.-> lab-117678{{"`Java Float Min Method`"}} java/modifiers -.-> lab-117678{{"`Java Float Min Method`"}} java/oop -.-> lab-117678{{"`Java Float Min Method`"}} java/identifier -.-> lab-117678{{"`Java Float Min Method`"}} java/arrays -.-> lab-117678{{"`Java Float Min Method`"}} java/comments -.-> lab-117678{{"`Java Float Min Method`"}} java/data_types -.-> lab-117678{{"`Java Float Min Method`"}} java/math -.-> lab-117678{{"`Java Float Min Method`"}} java/operators -.-> lab-117678{{"`Java Float Min Method`"}} java/output -.-> lab-117678{{"`Java Float Min Method`"}} java/strings -.-> lab-117678{{"`Java Float Min Method`"}} java/variables -.-> lab-117678{{"`Java Float Min Method`"}} java/math_methods -.-> lab-117678{{"`Java Float Min Method`"}} java/system_methods -.-> lab-117678{{"`Java Float Min Method`"}} end

Set up the project directory

Create a project directory named project in the home directory using the following command:

mkdir ~/project

Change directory to project directory using the following command:

cd ~/project

Create a Java file

To use min() method, we need to create a Java class. Run the following command to create a file named FloatMin.java.

touch FloatMin.java

Write the Java code

In this step, we will write the Java code that demonstrates the usage of min() method. Copy and paste the below code in the FloatMin.java file.

public class FloatMin {
    public static void main(String[] args) {
        float num1 = -27.56f;
        float num2 = -45.98f;
        float num3 = 68.3452f;
        float num4 = -94.456f;

        // Find the minimum of two numbers using Math.min() method
        float minNum1 = Math.min(num1, num2);
        float minNum2 = Math.min(num3, num4);

        System.out.println("Minimum of " + num1 + " and " + num2 + " is " + minNum1);
        System.out.println("Minimum of " + num3 + " and " + num4 + " is " + minNum2);
    }
}

Compile the Java file

Compile the FloatMin.java file using the following command:

javac FloatMin.java

Execute the Java program

Run the compiled code using the following command:

java FloatMin

You should see the following output:

Minimum of -27.56 and -45.98 is -45.98
Minimum of 68.3452 and -94.456 is -94.456

Summary

In this lab session, you have learned about the Java Float min() method and demonstrated its usage by finding the minimum value between two floating-point numbers.

Congratulations! You have successfully completed the Java Float min() Method lab.

Other Java Tutorials you may like