java.lang.Float min(float a,float b)

Description

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

Method Syntax

public static float min(float a,float b)

Method Argument

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

Method Returns

The min(float a,float b) method of Float class returns returns the smaller of two float values as if by calling Math.min.

Compatibility

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 min(float a, float b) value as of Java 8. we can now have the capability of getting the lowest 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 lowest.

Java Float float min(float a,float b) Example

Below is a simple java example on the usage of min(float a,float b) 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 
 * min(float a, float b) method of Float class.
 */

public class FloatMinExample {

	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 lowest number between the two input
		Float result = Float.min(f1, f2);
		System.out.println("The lowest 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 lowest using the min method.

Sample Output

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

Float min() Sample Output