Java max Function

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

Java math max Syntax

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

Math.max(data_type x, data_type y);
  • If the argument is positive or Negative integers, it will return the Output.
  • If we provide Positive Zero and Negative Zero as arguments, the 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 two given int arguments. The following function will accept positive or negative integer value as the first and second argument and returns the largest value of the 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 Java math max functions accept a negative or positive value of different data types as the first and second arguments. This function will return 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 Java Math.max function to find the maximum value from different data types and display the output.

In this example, First, We used the Function directly on both the Positive int or integer and negative integer values. The first three systems 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 variables b, c, d, and e (they belong 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 variables g, h, i, and j (they belong to the float type). The next three lines will call the float type method ( static float max(float a, float b) ) and find the largest.

Last, We used this Math function on variables k, l, m, and n (they belong to the long type). The last three statements will call the max function of the long type. Then find 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