In C
, there is a min()
convenience macro that helps make the code more readable.
In java, the shortest equivalent I know of is:
(a > b ? b : a)
But it's not as self-documenting as min()
in my view.
Is there a built-in min() method in Java (and I just couldn't find it)? or my only recourse is to define a method like this myself?
Collections. min() method return the minimum element in the specified collection and Collections. max () returns the maximum element in the specified collection, according to the natural ordering of its elements.
Overview. The max() method is an inbuilt method of Math class which is present in java. lang package that is used to find the maximum of two numbers. The max() method takes two inputs that are of types numbers, i.e., int, float, double, and returns the maximum of the given numbers.
min() function is an inbuilt function in java that returns the minimum of two numbers. The arguments are taken in int, double, float and long. If a negative and a positive number is passed as an argument then the negative result is generated.
Math.min()
? (Comes in several overloads for different types.)
have a look at the javadoc of Math
you can use it like :
import static java.lang.Math.*; public static void main(String[] args) { System.out.println(min(1, 0)); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With