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! :)