
The min and max Methods
The min and max methods return the smaller or larger of their two argu-
ments, respectively. Example 3.16 demonstrates how the min and max
methods can be used in a Java program. Figure 3.23 shows the output. Thus
the statement on line 9 of Example 3.16
int smaller = Math.min( 8, 2 );
will assign 2 to the int variable smaller. At line 12, a similar statement using
the max method will assign 8 to the int variable larger.
1 /* A demonstration of min and max Math class methods
2 Anderson, Franceschi
3 */
4
5 public class MathMinMaxMethods
6 {
7 public static void main( String [ ] args )
8 {
9 int smaller = Math.min( 8, 2 );
10 System.out.println ...