Java Float Max Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn about the max() method of the Float class in Java. This method returns the greater of two float values passed to it as parameters.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") 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/oop -.-> lab-117676{{"`Java Float Max Method`"}} java/packages_api -.-> lab-117676{{"`Java Float Max Method`"}} java/user_input -.-> lab-117676{{"`Java Float Max Method`"}} java/wrapper_classes -.-> lab-117676{{"`Java Float Max Method`"}} java/identifier -.-> lab-117676{{"`Java Float Max Method`"}} java/data_types -.-> lab-117676{{"`Java Float Max Method`"}} java/operators -.-> lab-117676{{"`Java Float Max Method`"}} java/output -.-> lab-117676{{"`Java Float Max Method`"}} java/strings -.-> lab-117676{{"`Java Float Max Method`"}} java/variables -.-> lab-117676{{"`Java Float Max Method`"}} java/system_methods -.-> lab-117676{{"`Java Float Max Method`"}} end

Create a new file named "FloatMax.java"

In the first step, create a new Java file named "FloatMax.java" in the ~/project directory.

touch ~/project/FloatMax.java

Import java.lang.Float

The max() method is a part of the Float class of the java.lang package. In order to use this method, you must include the java.lang.Float package in your code. Add the following line at the beginning of the "FloatMax.java" file:

import java.lang.Float;

Declare Test Values

Declare two float type variables and assign them some values. Here, we assign the variables a and b the values 7.5 and 6.2, respectively.

float a = 7.5f;
float b = 6.2f;

Use Float.max() to find the maximum of two float values

Use the Float.max() method to find the greater value of the two variables a and b. Assign the result to a third float type variable max.

float max = Float.max(a, b);

Print the Result

Finally, print the result using the System.out.println() method.

System.out.println("The maximum of " + a + " and " + b + " is " + max);

Run the code

Compile and run the "FloatMax.java" file using the following commands in the terminal:

cd ~/project
javac FloatMax.java
java FloatMax

Use Scanner class to get input from the user

Import the java.util.Scanner package. Create a new instance of the Scanner class to take input from the user. Read two float numbers from the user and store them in a and b variables, respectively.

Scanner scanner = new Scanner(System.in);
System.out.println("Enter a float number: ");
float a = scanner.nextFloat();
System.out.println("Enter another float number: ");
float b = scanner.nextFloat();

Use Float.max() to find the maximum of two float values

Use the Float.max() method to find the greater value of the two variables a and b. Assign the result to a third float type variable max.

float max = Float.max(a, b);

Print the Result

Finally, print the result using the System.out.println() method.

System.out.println("The maximum of " + a + " and " + b + " is " + max);

Summary

In this lab, you learned about the max() method of the Float class in Java. You also learned how to import packages, declare variables, use the System.out.println() method to print results, and how to take input from the user using the Scanner class. Now you can use the Float.max() method to find the greater value of any two float numbers you want!

Other Java Tutorials you may like