For a lot of utility-type methods, the apache commons libraries have solid implementations that you can either leverage or get additional insight from. In this case, there is a method for finding the smallest of three doubles available in org.apache.commons.lang.math.NumberUtils. Their implementation is actually nearly identical to your initial thought:

public static double min(double a, double b, double c) {
    return Math.min(Math.min(a, b), c);
}
Answer from wpgreenway on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-math-min-method-examples
Java Math min() method with Examples - GeeksforGeeks
January 9, 2026 - The value with the greater negative magnitude is considered smaller. Hence, -25 is returned as it is less than -23. If Math.min() is used frequently, you can statically import the method to avoid repeated class qualification. ... import static java.lang.Math.min; class GFG { public static void main(String[] args) { int a = 3; int b = 4; System.out.println(min(a, b)); } }
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › Math › min
Java Math min() - Get Minimum Value | Vultr Docs
September 27, 2024 - This snippet compares integers a and b. The Math.min() method identifies 3 as the smaller of the two, hence returning it. Consider two identical integer values.
🌐
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: ...
🌐
w3resource
w3resource.com › java-exercises › method › java-method-exercise-1.php
Java - Find the smallest number among three numbers
import java.util.Scanner; public class Exercise1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Input the first number: "); double x = in.nextDouble(); System.out.print("Input the Second number: "); double y = in.nextDouble(); System.out.print("Input the third number: "); double z = in.nextDouble(); System.out.print("The smallest value is " + smallest(x, y, z)+"\n" ); } public static double smallest(double x, double y, double z) { return Math.min(Math.min(x, y), z); } } Sample Output: Input the first number: 25 Input the Second number: 37 Input the third number: 29 The smallest value is 25.0 · Flowchart: For more Practice: Solve these Related Problems: Write a Java program to find the smallest number among three numbers using nested ternary operators.
🌐
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
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! ... Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
Find elsewhere
Top answer
1 of 5
57

To me it looks pretty readable

To me it doesn't.


Bugs:

The following method calls all incorrectly print 0:

System.out.println(min(3, 2, 2));
System.out.println(min(3, 3, 3));
System.out.println(min(1, 3, 3));
System.out.println(min(4, 2, 4));

This is because, when taking a look at your original code, it is overly complicated.

if( a < b && a < c && b < c) result = a ;
else if( a < b && a < c && b > c) result = a ;

Does it really matter if b < c or b > c? No, it doesn't here. And if b == c then neither of these current ones would be true which does the return 0. So that's a giant bug waiting to happen.

So those two first if's should be shortened into:

if (a <= b && a <= c) return a;

Note that I'm using <= here so that the case of all three having the same value gets handled correctly. Now also there's an early return so we don't need all these else.


If we group the rest of your statements according to what they return, we have for return b:

else if( a > b && a < c && b < c) result = b ;
else if( a > b && b < c && a > c) result = b ;

Which, if we always use b as the first operand and completely ignore whether or not a < c or a > c (and again, a == c is not handled here) we get:

if (b <= a && b <= c) return b;

Doing the same for c:

if (c <= a && c <= b) return c;

As one of a, b or c really has to be smallest, you can throw an exception if neither one of them is:

public static int min(int a, int b, int c) {
     if (a <= b && a <= c) return a;
     if (b <= a && b <= c) return b;
     if (c <= a && c <= b) return c;
     throw new AssertionError("No value is smallest, how did that happen?");
}

Or, if you don't like that exception:

public static int min(int a, int b, int c) {
     if (a <= b && a <= c) return a;
     if (b <= a && b <= c) return b;
     return c;
}

This is in my opinion significantly better than your original version.

However, I would recommend Pimgd's solution, either an array or using chained Math.min.

2 of 5
73

For these things we have java.lang.Math:

public static int min(final int a, final int b, final int c){
    return Math.min(a, Math.min(b, c));
}

Wow, look how short it is!

But it's 3 numbers today, it's 10 tomorrow.
As an alternative, how about an array?

public static int min(int... numbers){
    if (numbers.length == 0){
        throw new IllegalArgumentException("Can't determine smallest element in an empty set");
    }
    int smallest = numbers[0];
    for (int i = 1; i < numbers.length; i++){
        smallest = Math.min(smallest, numbers[i]);
    }
    return smallest;
}

I'd use the java.lang.Math solution, it's very short, and very readable.

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
🌐
Coderanch
coderanch.com › t › 592012 › java › find-min-numbers
find the min of 3 numbers (Beginning Java forum at Coderanch)
That's the simplest way to achive your task, and you'll have a method that works for any number of values you want to find min from. And please, be more specific in future when you post your questions. The quieter you are, the more you are able to hear. ... Winston Gutkowski wrote: The minimum of two numbers can be produced with a < b ? a : b so how do you think that might help to produce the minimum of 3?
🌐
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.
🌐
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 outcome will be the same if ... in java can be of the following types: int( for ex: 2,6,81), double (for ex: 22.4, 3.5), float( for ex: 2.65f, 7.89f), and long(for ex: 10l, 2l)....
🌐
Javatpoint
javatpoint.com › java-math-min-method
Java Math.min() method with Examples - Javatpoint
Java Math.min() method with Examples on abs(), min(), max(), avg(), round(), ceil(), floor(), pow(), sqrt(), sin(), cos(), tan(), exp() etc.
🌐
AlgoCademy
algocademy.com › link
Minimum Value Of Three in Java | AlgoCademy
Given 3 integers A, B and C, return the smallest number. You are not allowed to use built-in functions such as min
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-math-min-method
Java Math.min() Method
For example: -7 and -3, the min number is -7. public class JavaExample { public static void main(String args[]) { int a = 10, b = 20; int x = -10, y = -20; System.out.println("Minimum of a and b: "+Math.min(a,b)); System.out.println("Minimum of x and y: "+Math.min(x,y)); } } Output: public ...