What is NumberUtils.max in Java?
max() is a NumberUtils class that is used to find the maximum value among three values or in an array of values. The method is overloaded to support all the primitive types.
How to import NumberUtils
The definition of NumberUtils can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
For other versions of the
commons-langpackage, refer to the Maven Repository.
You can import the NumberUtils class as follows:
import org.apache.commons.lang3.math.NumberUtils;
Syntax
public static int max(final int... array)
Parameters
final int... array: The list of values.
Return value
This method returns the maximum value in the list of values.
Overloaded methods
public static int max(int a, final int b, final int c)
Code
import org.apache.commons.lang3.math.NumberUtils;import java.util.Arrays;public class Main{public static void main(String[] args) {int[] values = new int[]{3, 4, 2, 13, 2, 5434, 232, 43233};System.out.printf("NumberUtils.max(%s) = %s", Arrays.toString(values), NumberUtils.max(values));System.out.println();int a = 13;int b = 45;int c = 4;System.out.printf("NumberUtils.max(%s, %s, %s) = %s", a, b, c, NumberUtils.max(a, b, c));}}
Example 1
- values =
3, 4, 2, 13, 2, 5434, 232, 43233]
The method returns 43233 because it is the maximum value in the list of values.
Example 2
a = 13b = 45c = 4
The method returns 45 as it is the maximum value among the three values passed to the method.
Output
The output of the code will be as follows:
NumberUtils.max([3, 4, 2, 13, 2, 5434, 232, 43233]) = 43233
NumberUtils.max(13, 45, 4) = 45
Free Resources
- undefined by undefined