Minimum and Maximum MethodsThe Java math libraries contain two overloaded methods for computing the maximum or minimum of two input arguments. The return type of each method version is the same as the data type of the input arguments.
max() returns the greater of the input argument values. The method is overloaded to take double , float , int , and long arguments. If either value is NaN , the result is NaN .
min() returns the smaller of the input argument values. The method is overloaded to take double , float , int , and long arguments. If either value is NaN , the result is NaN . Example: Finding the Maximum of a Collection of DataIn this example, an array of type double contains a collection of width values. The max() method is used to determine the maximum width in the collection. public class MaxDemo { public static void main(String args[]) { double width[] = { 1.2, 2.3, 0.8, 1.7 }; double maxWidth; maxWidth = 0.0; for(int i=0; i<width.length; ++i) { maxWidth = Math.max(maxWidth, width[i]); } System.out.println("maximum width = "+maxWidth); } } Output ” maximum width = 2.3 |