Last Updated : 14 May, 2025
The max() method in Java is a part of java.lang.Math class. This is an inbuilt function in Java that returns maximum of two numbers. This method can work with any type of number, such as int, float, long, and double.
Special Cases:
dataType max(dataType num1, dataType num2)
Note: Here, data type can be int, float, long, or double.
Let's now discuss some examples for better understanding.
Example 1: In this example, we will see the basic usage of Math.max() method with Double values.
Java
// Java program to demonstrate
// the use of max() function
// when two double data-type
// numbers are passed as arguments
import java.lang.Math;
public class Geeks {
public static void main(String args[]) {
double a = 12.123;
double b = 12.456;
// prints the maximum of two numbers
System.out.println(Math.max(a, b));
}
}
Example 2: In this example, we will see the usage of Math.max() method with integer values including both positive and negative number.
Java
// Java program to demonstrate
// the use of max() function
// when one positive and one
// negative integer are passed
// as argument
import java.lang.Math;
public class Geeks {
public static void main(String args[]) {
int a = 23;
int b = -23;
// prints the maximum of two numbers
System.out.println(Math.max(a, b));
}
}
Example 3: In this example, we will see the usage of Math.max() method with two negative numbers.
Java
// Java program to demonstrate the
// use of max() function when two
// negative integers are passed as arguments
import java.lang.Math;
public class Geeks {
public static void main(String args[]) {
int a = -25;
int b = -23;
// prints the maximum of two numbers
System.out.println(Math.max(a, b));
}
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4