Description

The public static float max(float a, float b) method of Float class is use to get which of the two float is having the highest value. This method is a nice addition to Java API which has been recently added in Java 8.

Method Syntax

public static float max(float a, float b)

Method Argument

Data Type Parameter Description
float a the first operand
float b the second operand

Method Returns

The max(float a, float b)  method of Float class returns the greater of a and b.

Java 1.8

Discussion

In order to compare two float value, we usually use the compare and compareto method of Float class. And also if we are doing some complicated comparison, we can opt in in using our own comparator. With the addition of max(float a, float b) value as of Java 8. we can now have the capability of getting the greatest number between two float numbers. This is quite convenient to use, and make our code more readable. But make a note that this method is solely used to get which of the two float numbers is the greatest.

Java Float max(float a, float b) Example

Below is a simple java example on the usage of floatToIntBits(float value) method of Float class.

```package com.javatutorialhq.java.examples;

import java.util.Scanner;

import static java.lang.System.*;

/*
* This example source code demonstrates the use of
* max(float a, float b) method of Float class.
*/

public class FloatMaxExample {

public static void main(String[] args) {

// Ask user input for first number
System.out.print("Enter First Number:");
// declare the scanner object
Scanner scan = new Scanner(System.in);
// use scanner to get value from user console
Float f1 = scan.nextFloat();
// Ask user input for second number
System.out.print("Enter Second Number:");
Float f2 = scan.nextFloat();
// close the scanner object
scan.close();

// get the highest number between the two input
Float result = Float.max(f1, f2);
System.out.println("The highest number is: "+result);

}

}
```

Basically on the above example, we just ask for two float numbers from the console and we used the scanner class to get these values assigned as Float object. We then get which of the two float is the highest using the max method.

Sample Output

Below is the sample output when you run the above example.