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, Math.max function 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, Math.max function 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);

This math max function accepts a negative or positive double value as the first and second arg. It returns the Largest value of double type.

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

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

The following Java max function accepts positive or negative float value as the first and second arguments. It returns the maximum value of float type.

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

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

Below max function allows either positive or negative long value and returns the maximum value of Long type.

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

// In order to use in program: 
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.

// Java Math.max Function
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

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

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));

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

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));

Next, We used the Math.max Function on variable g, h, i, and j (they belong to float type). It will call the max method of float type ( static float max(float a, float b) ) and finds the largest.

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));

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

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));