Java Math max() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Math

Definition

In Java, Math.max(a, b) returns the larger of its two arguments. Math.max(3, 8) is 8.

Typical uses: tracking the highest score in a loop, and setting a lower limit, as in Math.max(0, stock - sold), so that a value never drops below zero.

A static method of the java.lang.Math class, available since the early versions of Java (JDK 1.0). Call it on the class name, as in Math.max(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass two numbers. If their types differ, Java widens the smaller type first.

Java
int best = Math.max(72, 95);               // 95
double top = Math.max(1.5, -2.0);          // 1.5
int left = Math.max(0, stock - sold);      // never below 0
int big3 = Math.max(a, Math.max(b, c));    // largest of three
CallWhat it does
Math.max(4, 9)int overload: 9.
Math.max(4, 9.0)Mixed: 4 widens to double, result 9.0.
Math.max(0, x)Lower limit: x, but never less than 0.
Math.max(a, Math.max(b, c))Largest of three values.

max takes exactly two arguments. For arrays, loop and keep a running maximum, or use Arrays.stream(arr).max() (Java 8+), which returns an optional result.

Library declarations
Java
// java.lang.Math
public static int max(int a, int b)
public static long max(long a, long b)
public static float max(float a, float b)
public static double max(double a, double b)

Parameters

ParameterDescription
aThe first value.
bThe second value.

Return Value

Return ValueDescription
Same type as the (widened) argumentsThe larger of a and b. If they are equal, that value.

Compare with Similar Methods

Methods and operators for picking or limiting values:

MethodHow it differs
min(a, b)Returns the smaller value; used for upper limits.
clamp(v, lo, hi)Lower and upper limit in one call. Java 21+.
a > b ? a : bSame for ints, but differs for NaN and -0.0.
Collections.max(list)Largest element of a whole collection.

Java Examples

Example 1 — Track the Highest Score in a Loop

Keep a running maximum while walking through an array, and nest calls for three values.

Java
public class MathMaxHighScore {
    public static void main(String[] args) {
        int[] scores = {72, 95, 64, 88};
        int best = Integer.MIN_VALUE;
        for (int s : scores) {
            best = Math.max(best, s);
        }
        System.out.println("Best score: " + best);
        System.out.println("Max of three: " + Math.max(10, Math.max(40, 25)));
    }
}

How It Works

  1. Start below everything. best begins at Integer.MIN_VALUE, so the first score always replaces it.
  2. Running maximum. Each pass keeps whichever is larger, the old best or the current score: 72, then 95, which 64 and 88 cannot beat.
  3. Nesting. The inner call picks 40 from 40 and 25, and the outer call compares that with 10.

Example 2 — Use max() as a Lower Limit

Stock and prices must not go negative. Math.max(0, ...) turns a negative result into zero.

Java
public class MathMaxNoNegative {
    public static void main(String[] args) {
        int stock = 5;
        int ordered = 8;
        int left = Math.max(0, stock - ordered);
        System.out.println("Items left: " + left);

        double price = 19.99;
        double coupon = 25.0;
        System.out.println("To pay: " + Math.max(0.0, price - coupon));
    }
}

How It Works

  1. A negative difference. 5 - 8 is -3, which makes no sense as a stock level.
  2. Zero wins. Math.max(0, -3) picks 0, so the shop shows zero items instead of minus three.
  3. Same idea for money. The coupon is worth more than the price, so the difference is negative and the customer pays 0.0 (a double, because both arguments are doubles).

Example 3 — Mixed Types, Negative Zero, and NaN

See how argument types decide the result type, and how max handles the unusual double values.

Java
public class MathMaxSpecial {
    public static void main(String[] args) {
        System.out.println("int vs double: " + Math.max(3, 9.0));
        System.out.println("long:          " + Math.max(5L, 2_000_000_000_000L));
        System.out.println("-0.0 vs 0.0:   " + Math.max(-0.0, 0.0));
        System.out.println("NaN vs 100:    " + Math.max(Double.NaN, 100.0));
    }
}

How It Works

  1. Widening. One argument is a double, so 3 becomes 3.0 and the double overload returns 9.0 with a decimal point.
  2. Signed zeros are ordered. The two zeros are equal for ==, but max deliberately treats 0.0 as the larger one.
  3. NaN is contagious. A NaN argument means "unknown", so the maximum is unknown too, even next to 100.0.

Frequently Asked Questions

The larger of its two arguments, in the same type as the arguments. Math.max(3, 8) returns the int 8.
Nest the calls: Math.max(a, Math.max(b, c)). For many values, loop over them and keep a running maximum.
Start with the first element or Integer.MIN_VALUE, then update it with best = Math.max(best, x) for every element. With streams you can also use Arrays.stream(arr).max().getAsInt().
NaN. Any comparison involving NaN is unknown, so the result is NaN too.
Yes. The int is widened to double and the double version runs, so Math.max(3, 9.0) returns 9.0.

Next: min()

The smaller of two int, long, float, or double values.

min() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful