Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Math.min() and Math.max(): should they still be limited to two arguments?

936169May 9 2012 — edited May 15 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 min(int a, int b) {
return (a <= b) ? a : b;
}

we could have something like this:

public static int min(final int first, final int... others) {
if ( (others==null) || (others.length==0) ) {
return first;
}

int minimum=first;
for (int a : others) {
if (a < minimum) {
minimum=a;
}
}

return minimum;
}

And simplify everyone's life by allowing calls like max(1, 2, 3, 4).

Yes, there are some issues to work out (for example, the method above won't accept min(x), where x is a int[] - a usage most programmers might expect to be accepted), but I think this would be a positive addition to the Math library (and StrictMath as well).

I have no idea if this has been discussed to death before, so I'd like ask around first. What do you think?

Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 12 2012
Added on May 9 2012
24 comments
1,060 views