Java max Function

The Java max Function is one of the Java Math Library functions used to return the Maximum or Largest value from the given two arguments.

Java max Function Syntax

The syntax of the Math.max in Java Programming language is

Math.max(data_type x, data_type y);
  • If the argument is either positive integers or Negative integers, it will return the Output.
  • If we provide Positive Zero and Negative Zero as arguments, Java Math max Function will return the result as Positive Zero.
  • Moreover, if the arguments are not a number, it will return NaN.

Java Programming provides four different Math.max functions to find the Maximum or Largest value from the given two arguments. The following Java max int function will accept positive or negative integer value as the first and second argument and returns the Largest value or largest value of integer type.

static int max(integer x, intger y); //Return Type is Integer

// In order to use in program: 
Math.max(int x, int y);

Similarly, the following math max functions accepts a negative or positive value of different data types as the first and second arguments. The Java math max function will returns the Largest value of double, float, and long datatypes.

static double max(double x, double y); //Return Type is double
Math.max(double x, double y);

static float max(float x, float y); //Return Type is float
Math.max(float x, float y);

static long max(long x, long y); //Return Type is long
Math.max(long x, long y);

Java max Function Example

In this example, we use the Math.max function to find the maximum value from a different set of data types and display the output.

In this example, First, We used the Math.max Function directly on both the Positive int or integer and negative integer values. The first three system out println statements will call the integer type ( static int max(integer a, integer b) ) method and find the largest among the values.

Here, we used the Java Math.max Function on variable b, c, d, and e (they belongs to double type). The following three statements will call the method of double type ( static double max(double a, double b) ) and find the largest among the values.

Next, We used it on variable g, h, i, and j (they belong to float type). The next three lines will call the float type method ( static float max(float a, float b) ) and finds the largest.

Last, We used this Math function on variable k, l, m, and n (they belong to long type). The last three statements will call the Java max function of long type. Then finds the largest among the values.

package MathFunctions;

public class MaxMethod {
 public static void main(String[] args) {
 double b = 10.9666, c = 14.6674, d = -9.474, e = -14.9865;
 float g = 1.23f, h = 4.809f, i = -7.89f, j = -6.54f;
 long  k = 200, l = 400, m = -300, n = -100;
 
 System.out.println("Maximum value of Positive Integer: " + Math.max(10, 20));
 System.out.println("Maximum value of Negative Integer: " + Math.max(-20, -60));
 System.out.println("Maximum value of both Positive & Negative: " + Math.max(10, -20));
 
 System.out.println("\nMaximum value of Positive Double: " + Math.max(b, c));
 System.out.println("Maximum value of Negative Double: " + Math.max(d, e));
 System.out.println("Maximum value of both Positive & Negative: " + Math.max(b, e));
 
 System.out.println("\nMaximum value of Positive Float: " + Math.max(g, h));
 System.out.println("Maximum value of Negative Float: " + Math.max(i, j));
 System.out.println("Maximum value of both Positive & Negative: " + Math.max(h, i));
 
 System.out.println("\nMaximum value of Positive Number: " + Math.max(k, l));
 System.out.println("Maximum value of Negative Number: " + Math.max(m, n));
 System.out.println("Maximum value of both Positive & Negative: " + Math.max(k, n));
 }
}
Java max Function 1