Example of Basic Usage
Let's start with a simple example to understand how Math.max works.
Java
public class MaxExample {
public static void main(String[] args) {
int num1 = 10;
int num2 = 20;
int result = Math.max(num1, num2);
System.out.println("The maximum value is: " + result);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
The maximum value is: 20
In this example, Math.max compares num1 and num2 and returns the larger value, which is 20.
Using Math.max with Different Data Types
Math.max with int
Java
int maxInt = Math.max(5, 10);
System.out.println("Max int: " + maxInt);

You can also try this code with Online Java Compiler
Run Code
Output
Max int: 10
Math.max with float
java
Java
float maxFloat = Math.max(5.5f, 10.5f);
System.out.println("Max float: " + maxFloat);

You can also try this code with Online Java Compiler
Run Code
Output
Max float: 10.5
Math.max with double
Java
double maxDouble = Math.max(5.5, 10.5);
System.out.println("Max double: " + maxDouble);

You can also try this code with Online Java Compiler
Run Code
Output
Max double: 10.5
Math.max in Conditional Statements
Using Math.max in conditional statements can simplify the code and make it more readable.
Example with if-else
Java
int a = 15;
int b = 30;
int max;
if (a > b) {
max = a;
} else {
max = b;
}
System.out.println("Max value: " + max);

You can also try this code with Online Java Compiler
Run Code
Output
Max value: 30
Using Math.max, the same logic becomes simpler:
int max = Math.max(a, b);
System.out.println("Max value: " + max);
Math.max in Loops
Finding Maximum in an Array
You can use Math.max to find the maximum value in an array.
Java
public class MaxInArray {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
int max = numbers[0];
for (int num : numbers) {
max = Math.max(max, num);
}
System.out.println("The maximum value in the array is: " + max);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
The maximum value in the array is: 50
Frequently Asked Questions
How do I use Math.max with more than two numbers?
To find the maximum among multiple numbers, you can nest Math.max calls.
int max = Math.max(Math.max(a, b), c);
Can I use Math.max with arrays?
You can use a loop to iterate through the array and find the maximum value.
What happens if the values are equal?
Math.max will return the value itself if both are equal.
Conclusion
The Math.max method in Java is a simple yet powerful tool for comparing two numbers and finding the maximum value. It is versatile, supporting various data types, and can be used in different scenarios, from basic comparisons to complex logic in loops. By understanding and using Math.max, you can write cleaner, more efficient code. Remember to follow best practices and consider performance when working with larger datasets.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.