Home
Map
Math.max and Math.minUse Math.min and Math.max to compare values and keep values in ranges. Math.min can be used with array indexes.
Java
This page was last reviewed on Dec 5, 2023.
Math.max and min. Two numbers are different—one is larger and the other is smaller. With Math.max we get the larger one, and with Math.min we get the smaller one.
Math
With these methods, we can restrict numbers to a valid range. We can avoid accessing an array out of range. We can keep track of the highest (or lowest) score.
First example. Here we use Math.min. We pass it two arguments and receive the argument back that is lower. We use Math.min to avoid accessing an array at an index that is too high.
Array
Argument 1 In this example we use the last valid index of the "animals" array. We can never go higher than the last index.
Argument 2 This is the element we want to access. If this is too high, we will instead access the last valid index of the array.
public class Program { public static void main(String[] args) { String[] animals = { "cat", "dog", "bird" }; // Use Math.min to keep index in range. // ... Try to get index 4. String result = animals[Math.min(animals.length - 1, 4)]; System.out.println(result); // Try to get index 1. result = animals[Math.min(animals.length - 1, 1)]; System.out.println(result); } }
bird dog
Array for-loop. In this example we have two arrays: one has 3 elements, the other 4. With Math.min we get the count of common element indexes in both arrays.
So The Math.min method returns 3 here. Both codes1 and codes2 have 3 elements.
Finally We print the common elements in a for-loop. The fourth element in "codes2" is not printed.
public class Program { public static void main(String[] args) { int[] codes1 = { 100, 200, 300 }; int[] codes2 = { 300, 400, 500, 600 }; // Use Math.min to get smallest length for both arrays. int commonLength = Math.min(codes1.length, codes2.length); // Display elements in each array at indexes. // ... All indexes have elements. for (int i = 0; i < commonLength; i++) { System.out.println(codes1[i] + "/" + codes2[i]); } } }
100/300 200/400 300/500
Math.max. In this example we score words based on their letters. We use Math.max to keep track of the highest-scoring word. We do not need if-statements to track this value.
String charAt
Tip Often we assign a variable to the result of Math.max that is passed (as one argument) that same value.
And This helps us modify a variable based on local maximums and minimums for Math.min.
public class Program { public static void main(String[] args) { String[] codes = { "abc", "def", "abcdef", "bc" }; // Loop over strings and score them on their letters. int maxScore = -1; for (String code : codes) { // Compute score for the string. int score = 0; for (int i = 0; i < code.length(); i++) { score += code.charAt(i); } System.out.println(score); // Keep track of highest score with Math.max. maxScore = Math.max(maxScore, score); } // Print highest score. System.out.println("Max score: " + maxScore); } }
294 303 597 197 Max score: 597
A review. With Math.max and Math.min we compare two numbers in a method call. We can avoid writing many if-statements. This leads to clearer, more correct, more declarative code.
if
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 5, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.