int

public static int min(int a, int b) {
    return (a <= b) ? a : b;
}

long

public static long min(long a, long b) {
     return (a <= b) ? a : b;
}

float

public static float min(float a, float b) {
    if (a != a) return a;   // a is NaN
    if ((a == 0.0f) && (b == 0.0f) && (Float.floatToIntBits(b) == negativeZeroFloatBits)) {
         return b;
    }
    return (a <= b) ? a : b;
 }

double

public static double min(double a, double b) {
    if (a != a) return a;   // a is NaN
    if ((a == 0.0d) && (b == 0.0d) && (Double.doubleToLongBits(b) == negativeZeroDoubleBits)) {
        return b;
    }
    return (a <= b) ? a : b;
}

More info: Here

Answer from Gray on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_math_min.asp
Java Math min() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ยท โฎ Math Methods ยท Get the lowest value from different pairs of numbers: ...
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ Math.html
Math (Java Platform SE 8 )
October 20, 2025 - If the argument is zero, the result is -Float.MIN_VALUE ... The adjacent floating-point value closer to negative infinity. ... Returns d ร— 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the double value set. See the Java Language Specification for a discussion of floating-point value sets.
Discussions

java - How does math.min actually work? - Stack Overflow
I understand that all of the math functions in java are built in. But I was wondering out of curiosity how Math.min() actually works? I checked the java documentation and couldn't find anything to... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to use "Math.min" and "Math.max"?
Did you even look at the API Docs? http://docs.oracle.com/javase/6/docs/api/ Math.min requires two arguments, not 3. When in doubt, look at the function you are calling and see what it does, otherwise you're just "guessing" which leads to bad habits. bholzer's code should suffice. The next question would be, do you know why his code fixes the problem? More on reddit.com
๐ŸŒ r/learnprogramming
7
12
September 3, 2012
java - How to use Math.min and Math.max in integer array - Stack Overflow
I want to find the maximum and minimum values in an integer array but I'm unable to use them . Eclipse throws this error The method min(int, int) in the type Math is not applicable for the More on stackoverflow.com
๐ŸŒ stackoverflow.com
Math.min() and Math.max(): should they still be limited to ...
Math.min() and Math.max() take exactly two arguments each. Since Java 5, both methods could very well take varargs, allowing programmers to write simpler code. So, instead of this: public static int ... More on forums.oracle.com
๐ŸŒ forums.oracle.com
May 9, 2012
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-math-min-method-examples
Java Math min() method with Examples - GeeksforGeeks
January 9, 2026 - Therefore, -23 is returned as the minimum value. Math.min() compares two negative values.
Top answer
1 of 6
22

int

public static int min(int a, int b) {
    return (a <= b) ? a : b;
}

long

public static long min(long a, long b) {
     return (a <= b) ? a : b;
}

float

public static float min(float a, float b) {
    if (a != a) return a;   // a is NaN
    if ((a == 0.0f) && (b == 0.0f) && (Float.floatToIntBits(b) == negativeZeroFloatBits)) {
         return b;
    }
    return (a <= b) ? a : b;
 }

double

public static double min(double a, double b) {
    if (a != a) return a;   // a is NaN
    if ((a == 0.0d) && (b == 0.0d) && (Double.doubleToLongBits(b) == negativeZeroDoubleBits)) {
        return b;
    }
    return (a <= b) ? a : b;
}

More info: Here

2 of 6
10

Java 7 documentation:

Returns the smaller of two int values. That is, the result the argument closer to the value of Integer.MIN_VALUE. If the arguments have the same value, the result is that same value.

Behaviour:

Math.min(1, 2) => 1
Math.min(1F, 2) => 1F
Math.min(3D, 2F) => 2D
Math.min(-0F, 0F) => -0F
Math.min(0D, -0D) => -0D
Math.min(Float.NaN, -2) => Float.NaN
Math.min(-2F, Double.NaN) => Double.NaN

java.lang.Math and java.lang.StrictMath Source:

public static int min(int a, int b) {
    return (a <= b) ? a : b;
}

java.lang.Math Bytecode (javap -c Math.class of Oracle's JDK's JRE's rt.jar):

public static int min(int, int);
Code:
   0: iload_0           // loads a onto the stack
   1: iload_1           // loads b onto the stack
   2: if_icmpgt     9   // pops two ints (a, b) from the stack
                        // and compares them
                        // if b>a, the jvm continues at label 9
                        // else, at the next instruction, 5
                        // icmpgt is for integer-compare-greater-than
   5: iload_0           // loads a onto the stack
   6: goto          10  // jumps to label 10
   9: iload_1           // loads 
  10: ireturn           // returns the currently loaded integer

If the comparison at 5 is true, a will be loaded, the jvm will jump to 10 and return a, if the comparison yields false, it will jump to 9, which will load and return b.

Intrinsics:

This .hpp file of the Java 8 Hotspot JVM hints that it optimizes Math.min even further with optimized machine code:

do_intrinsic(_min, java_lang_Math, min_name, int2_int_signature, F_S)

This means the above bytecode won't be executed by the Java 8 Hotspot JVM. However, this differs from JVM to JVM, which is why I also explained the bytecode!

Hopefully, now you know all there is to know about Math.min! :)

๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ library โ€บ math โ€บ min
Java Math min()
Become a certified Java programmer. Try Programiz PRO! ... The min() method returns the smaller value among the specified arguments. class Main { public static void main(String[] args) { // returns minimum of 25 and 31 System.out.println(Math.min(25, 31)); } } // Output: 25
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ standard-library โ€บ java โ€บ lang โ€บ Math โ€บ min
Java Math min() - Get Minimum Value | Vultr Docs
September 27, 2024 - The Math.min() function in Java serves as a simple yet powerful tool for determining the minimum value between two inputs. Whether dealing with integers, floating points, handling negative values, or involved in more complex expressions, this ...
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ how to use "math.min" and "math.max"?
r/learnprogramming on Reddit: How to use "Math.min" and "Math.max"?
September 3, 2012 -

I'm trying to write a program that takes three int values from the command line and prints them in ascending order, using Math.min() and Math.max(). But I keep getting an error message due to:

    int min    = Math.min(a, b, c);
    int max    = Math.max(a, b, c);
    int median = a + b + c - min - max;

I know that I've written it wrong inside the Maths parentheses, I've tried so many ways including: a "+" b "+" c, a + b +c etc. What am I doing wrong?

๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ java math โ€บ java math.min() method
Java Math.min() method
September 28, 2023 - At line 8, double min = Math.min(berriesSoldInKg, cherriesSoldInKg); the double โ€œminโ€ stores the lowest of both weights. Later, we compare two doubles (amount in kgs) to check the minimum of the two fruits.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ java/lang โ€บ java math min method
Java Math Min Method
September 1, 2008 - The Java Math min(int a, int b) returns the smaller of two int values. That is, the result is the value closer to negative infinity. If the arguments have the same value, the result is that same value.
๐ŸŒ
DaniWeb
daniweb.com โ€บ programming โ€บ software-development โ€บ threads โ€บ 172908 โ€บ max-and-min-of-more-than-2-numbers
java - Max and Min of more than 2 numbers | DaniWeb
February 2, 2009 - Math.min/max only compare two values at a time, so to handle N values you iterate once, carrying the current min and max. Initialize both to the first value to correctly handle all-negative inputs and to avoid guessing sentinel extremes.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ java โ€บ math methods โ€บ .min()
Java | Math Methods | .min() | Codecademy
October 22, 2022 - The Math.min() method returns the minimum value from the given two arguments. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ lang โ€บ math_min_long.htm
Java - Math min(long x, long y) Method
The Java Math min(long a, long b) returns the smaller of two long values. That is, the result is the value closer to negative infinity. If the arguments have the same value, the result is that same value.
๐ŸŒ
Quora
quora.com โ€บ Can-Math-min-have-only-two-parameters-in-Java
Can Math.min have only two parameters in Java? - Quora
Answer (1 of 2): Sometimes I donโ€™t know wether the guys asking the questions here are lazy, stupid, trolls or just plain blank. Look at Math (Java Platform SE 7 ) You will find that Math.min() has 4 implementations, each only hold 2 arguments. If you need more arguments you will have to do you...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ min() in java
min() in Java - Scaler Topics
April 8, 2024 - The Math.min() is a built-in method in Java that which accepts two parameters and returns the minimum of the two.
๐ŸŒ
Oracle
forums.oracle.com โ€บ ords โ€บ apexds โ€บ post โ€บ math-min-and-math-max-should-they-still-be-limited-to-two-a-1592
Math.min() and Math.max(): should they still be limited to ...
May 9, 2012 - Math.min() and Math.max() take exactly two arguments each. Since Java 5, both methods could very well take varargs, allowing programmers to write simpler code. So, instead of this: public static int ...
๐ŸŒ
JavaBeat
javabeat.net โ€บ home โ€บ how to use math.min() method in java?
How to Use Math.min() Method in Java?
November 30, 2023 - To use the โ€œMath.min()โ€ method in Java, pass two variables from which the minimum value needs to be found as arguments and the method returns the smallest value argument. If one of the operand values is negative, then the negative value will be returned and it can be worked with all primitive data types like int, float, double, and long.
Top answer
1 of 4
103

Floating-point numbers are way more complicated than integer values.

For this specific case two distinctions are important:

  • NaN is a valid value for float and double which represents "not a number" and behaves weirdly. Namely, it doesn't compare equal to itself.
  • Floating point numbers can differentiate between 0.0 and -0.0. A negative zero could conceivably be useful when you're calculating the limit of some function. Distinguishing whether a limit approaches 0 from the positive or the negative direction could be beneficial.

So this part:

if (a != a) {
      return a;
}

ensures that NaN is returned if a is NaN (if a is not NaN, but b is, then the "normal" check later on will return b, i.e. NaN, so no explicit check is needed for this case). This is a common pattern: when calculating anything where one input is NaN, the output will also be NaN. Since NaN usually represents some error in the calculation (such as dividing 0 by 0), it's important that it "poisons" all further calculations to ensure the error isn't silently swallowed.

This part:

if (a == 0.0F && b == 0.0F && (long)Float.floatToRawIntBits(b) == negativeZeroFloatBits) {
      return b;
}

ensures that if you compare two zero-valued floating point numbers and b is negative zero then that negative zero is returned (since -0.0 is "smaller" than 0.0). Similarly to NaN the normal check will correctly return a if it's -0.0 and b is 0.0.

2 of 4
40

I recommend carefully reading the documentation for Math.min and also the numeric comparison operators on floating points. Their behaviours are quite different.

Relevant parts from Math.min:

If either value is NaN, then the result is NaN. Unlike the numerical comparison operators, this method considers negative zero to be strictly smaller than positive zero.

and from JLS ยง15.20.1 "Numerical Comparison Operators <, <=, >, and >="

The result of a floating-point comparison, as determined by the specification of the IEEE 754 standard, is:

  • If either operand is NaN, then the result is false.

  • Positive zero and negative zero are considered equal.

If any argument is NaN, Math.min picks that one, but if any operand is NaN, <= evaluates to false. This is why it has to check if a not equal to itself - this would mean a is NaN. If a is not NaN but b is, the last case would cover it.

Math.min also considers -0.0 to be "less than" +0.0, but the numeric comparison operators think they are equal. This is the purpose of the second check.