math

Find the maximum of numbers with Math.max in Java

In this example, we shall show you how to find the maximum numbers in Java, using the Math Class and the method Math.max.

1. About Math Class

The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.

By using the associated functions directly, you can skip creating the logic of all the functions. Every mathematical operation/function is in one way or the other part of the Math Class. Few of the examples include sin(), tan(), cos(), abs(),log(), log10() and many others.

Among all the mentioned above methods, one such method is max(). Let us read more to understand what it does.

2. Math.max method in Java

Math.max() is used to find out the largest of two numbers. It takes two parameters and returns the larger of the two. The data type of the returning variable is the same as the input parameters.

Use the max(double a, double b), max(float a, float b), max(int a, int b), max(long a, long b) API methods of Math according to the types of arguments to get the greater of the two values, as described in the code snippet below.

3. Math.max examples in Java

In the first example below, we will be demonstrating the usage of Math.max() with different data types.

Example 1

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
package com.javacodegeeks.snippets.core;
 
public class FindMaximumOfNumbersWithMathMax {
     
    public static void main(String[] args) {
         
        // maximum of two integers
        System.out.println(Math.max(10,50));
          
        // maximum of two float values
        System.out.println(Math.max(21.64f,56.8f));
          
        // maximum of two double values
        System.out.println(Math.max(15.94,23.15));
          
        // maximum of two long values
        System.out.println(Math.max(1234L,9876L));
         
    }
 
}

Output

50
56.8
23.15
9876

In the second example below, we will be using Math.max()with positive and negative values.

Example 2

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
package com.javacodegeeks.snippets.core;
 
public class FindMaximumOfNumbersWithMathMax {
     
    public static void main(String[] args) {
         
        maximum of two positive integers
        System.out.println(Math.max(10,50));
          
        // maximum of two negative values
        System.out.println(Math.max(-10,-50));
          
        // maximum of one positive and one negative value
        System.out.println(Math.max(-10,50));
          
         
    }
 
}

Output

50
-10
50

4. Summary

This was an example of how to find the maximum numbers in Java, using the Math class and the method Math.max().

5. Download the Source Code

You can download the code used in the examples above.

Download
You can download the full source code of this example here: Find the maximum of numbers with Math.max in Java

Updated on Sept. 30th, 2020

Simran Koul

Simran has graduated as a Bachelor of Engineering in Computer Science from Chitkara University. She has undergone a 6-months long comprehensive industrial training at the reputed Centre for Development of Advanced Computing (C-DAC), where she worked on a project including the likes of Java, JSP, Servlets while the UI-UX through the pioneering HTML, CSS and JS. Her inquisitive nature and the seed of curiosity keeps her on the toes to find material to write about. Along with her interests in Software Development, she is an ardent reader and always ready-to-write writer.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button