Java Math.max Method

The purpose of the Java Math.max() method is to find the largest of the two numbers. This numbers may be positive or negative.

Java Math.max Method

Java Math.max() method can be used with four data types (int, double, float, long) as input arguments.

java math.max method

Example 1: Find the largest of the two numbers 8.4685 and 8.46851

double num1 = 8.4685; 
double num2 = 8.46851; 

double max = Math.max(num1, num2);
System.out.println(max); //8.46851

Example 2: Find the largest of the two numbers 5 and -5 (one positive and one negative value).

int num1 = 5; 
int num2 = -5; 

int max = Math.max(num1, num2);
System.out.println(max); //5

Example 3: Find the largest of the two numbers -5 and -50 (two negative numbers).

int num1 = -5; 
int num2 = -50; 

int max = Math.max(num1, num2);
System.out.println(max); //-5