I don't seem to understand how Integer.MAX_VALUE
and Integer.MIN_VALUE
help in finding the min and max value in an array.
I understand how this method (pseudocode below) works when finding the min and max values:
max = A[0], min = A[0] for each i in A if A[i] > max then max = A[i] if A[i] < min then min = A[i]
But as for this method, I don't understand the purpose of Integer.MAX_VALUE
and Integer.MIN_VALUE
:
import java.util.Scanner; class MyClass { public static void main(String[] args) { int[] numbers; // declaring the data type of numbers numbers = new int[3]; //assigning the number of values numbers will contain int smallest = Integer.MAX_VALUE, largest = Integer.MIN_VALUE; Scanner input = new Scanner(System.in); System.out.println("Please enter 3 numbers"); for(int counter = 0; counter<numbers.length;counter++) { numbers[counter] = input.nextInt(); } for(int i = 0; i<numbers.length; i++) { if(numbers[i]<smallest) smallest = numbers[i]; else if(numbers[i]>largest) largest = numbers[i]; } System.out.println("Largest is "+largest); System.out.println("Smallest is "+smallest); } }
So what purpose do Integer.MIN_VALUE and Integer.MIN_VALUE serve in the comparisons?
MIN_VALUE. The Integer. MIN_VALUE is a constant in the Integer class that represents the minimum or least integer value that can be represented in 32 bits, which is -2147483648, -231. This is the lowest value that any integer variable in Java can hold.
Integer. MAX_VALUE represents the maximum positive integer value that can be represented in 32 bits (i.e., 2147483647 ). This means that no number of type Integer that is greater than 2147483647 can exist in Java.
The int type in Java can be used to represent any whole number from -2147483648 to 2147483647. Why those numbers? Integers in Java are represented in 2's complement binary and each integer gets 32 bits of space.
but as for this method, I don't understand the purpose of Integer.MAX_VALUE and Integer.MIN_VALUE.
By starting out with smallest
set to Integer.MAX_VALUE
and largest
set to Integer.MIN_VALUE
, they don't have to worry later about the special case where smallest
and largest
don't have a value yet. If the data I'm looking through has a 10
as the first value, then numbers[i]<smallest
will be true (because 10
is <
Integer.MAX_VALUE
) and we'll update smallest
to be 10
. Similarly, numbers[i]>largest
will be true
because 10
is >
Integer.MIN_VALUE
and we'll update largest
. And so on.
Of course, when doing this, you must ensure that you have at least one value in the data you're looking at. Otherwise, you end up with apocryphal numbers in smallest
and largest
.
Note the point Onome Sotu makes in the comments:
...if the first item in the array is larger than the rest, then the largest item will always be Integer.MIN_VALUE because of the else-if statement.
Which is true; here's a simpler example demonstrating the problem (live copy):
public class Example { public static void main(String[] args) throws Exception { int[] values = {5, 1, 2}; int smallest = Integer.MAX_VALUE; int largest = Integer.MIN_VALUE; for (int value : values) { if (value < smallest) { smallest = value; } else if (value > largest) { largest = value; } } System.out.println(smallest + ", " + largest); // 1, 2 -- WRONG } }
To fix it, either:
Don't use else
, or
Start with smallest
and largest
equal to the first element, and then loop the remaining elements, keeping the else if
.
Here's an example of that second one (live copy):
public class Example { public static void main(String[] args) throws Exception { int[] values = {5, 1, 2}; int smallest = values[0]; int largest = values[0]; for (int n = 1; n < values.length; ++n) { int value = values[n]; if (value < smallest) { smallest = value; } else if (value > largest) { largest = value; } } System.out.println(smallest + ", " + largest); // 1, 5 } }
Instead of initializing the variables with arbitrary values (for example int smallest = 9999, largest = 0
) it is safer to initialize the variables with the largest and smallest values representable by that number type (that is int smallest = Integer.MAX_VALUE, largest = Integer.MIN_VALUE
).
Since your integer array cannot contain a value larger than Integer.MAX_VALUE
and smaller than Integer.MIN_VALUE
your code works across all edge cases.
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