Note that a is Integer.MAX_VALUE - 1 and b is Integer.MIN_VALUE + 1. So yes, it is indeed subtracting and adding 1 twice in each case. The book is not wrong, but it's a stupid way of teaching about wrap-around overflow. Just printing Integer.MIN_VALUE - 1 and Integer.MAX_VALUE + 1 would have made the point.

int min = Integer.MIN_VALUE -1; // min is set to Integer.MAX_VALUE by underflow
int max = Integer.MAX_VALUE +1; // max is set to Integer.MIN_VALUE by overflow

From the Java Language Specification, §15.18.2:

If an integer addition overflows, then the result is the low-order bits of the mathematical sum as represented in some sufficiently large two's-complement format.

The JLS is the ultimate authority when it comes to questions like this, but I don't recommend reading it as a way to learn Java. You'd be better off going through the Java Language Tutorial. It's fairly comprehensive and the content is very high quality.

Answer from Ted Hopp on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › integer-max_value-and-integer-min_value-in-java-with-examples
Integer.MAX_VALUE and Integer.MIN_VALUE in Java with Examples - GeeksforGeeks
July 12, 2025 - Integer.MIN_VALUE Integer.MIN_VALUE is a constant in the Integer class of java.lang package that specifies that stores the minimum possible value for any integer variable in Java.
🌐
Educative
educative.io › answers › what-is-integerminvalue-in-java
What is Integer.MIN_VALUE in Java?
We can encode that many values in 32 bits of space, where one bit represents the sign. 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 ...
🌐
Scaler
scaler.com › home › topics › what is integer.min_value in java?
What is Integer.Min_Value in Java? - Scaler Topics
September 22, 2023 - It is the bottommost limit for any integer variable in the Java programming language. ... Any value less than Integer.MIN_VALUE will lead to an overflow in memory and will be a positive value.
🌐
TutorialsPoint
tutorialspoint.com › integer-max-value-and-integer-min-value-in-java-with-examples
Integer.MAX_VALUE and Integer.MIN_VALUE in Java with Examples
July 20, 2023 - The Integer class of Java provides ... Integer.MAX_VALUE is 231 -1 which is equivalent to 2147483647 and the actual value of Integer.MIN_VALUE is -231 which is equivalent to -2147483648....
🌐
LabEx
labex.io › tutorials › java-java-integer-min-method-117722
Mastering the Java Integer Min Method | LabEx
In this lab, you will learn about the min() method of the Integer class in Java. This method is used to return the numerically smaller value(minimum value) of the two numbers passed as arguments.
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java collections › finding max/min of a list or collection
Finding Max/Min of a List or Collection | Baeldung
April 4, 2025 - A quick and practical guide to getting maximum/minimum element from a list or a collection.
🌐
Reddit
reddit.com › r/learnjava › beginner here; please help with explaining why integer.max_value and integer.min_value is assigned as values in this code.
r/learnjava on Reddit: Beginner here; please help with explaining why Integer.MAX_VALUE and Integer.MIN_VALUE is assigned as values in this code.
July 4, 2022 -

I was searching for a way to find the min and max value within a number of integers and I came across this code:

Scanner in = new Scanner(System.in);

int maxNum = Integer.MIN_VALUE;
int minNum = Integer.MAX_VALUE;
while (scanner.hasNextInt()) {
  int num = scanner.nextInt();
  maxNum = Math.max(maxNum, num);
  minNum = Math.min(minNum, num);
}

System.out.println("The maximum number: " + maxNum);
System.out.println("The minimum number: " + minNum);

I am struggling to wrap my mind around Integer.MAX_VALUE and Integer.MIN_VALUE. Why are they assigned as values and how does it work with the Math. min & max methods. Why is the variable maxNum assigned the value Integer.MIN_VALUE and minNum assigned the value Integer.MAX_VALUE?

Thanks.

Top answer
1 of 3
3
Think of only one, you are looking for the lowest number in an array, you take the first number and compare it to your minValue, if it is smaller you choose that as new minValue and try the next. If you start minValue as the absolute MIN_VALUE, it will never find a smaller number in your array. SO you should start minValue as higher as possible and maxValue as lover. (Or you can just initiate them as the first number in your array)
2 of 3
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Integer.html
Integer (Java Platform SE 8 )
October 20, 2025 - Returns the value obtained by reversing the order of the bytes in the two's complement representation of the specified int value. ... Adds two integers together as per the + operator. ... Returns the greater of two int values as if by calling Math.max. ... Returns the smaller of two int values as if by calling Math.min. ... Java™ Platform Standard Ed.
Top answer
1 of 3
5

The short answer is that this is how twos-complement negation works. It's not overflow, and it wouldn't be detectable without special circuitry in the processor (or equivalent checks in the language runtime).

How Twos-Complement Arithmetic Works

I'll start with the number line, in binary, limiting my wordsize to 3 bits:

011 =  3
010 =  2
001 =  1
000 =  0
111 = -1
110 = -2
101 = -3
100 = -4

Inside the computer, there is an Adder circuit that can combine two bits and produce a result plus carry bit. These circuits are chained together, so that the processor can add two entire words. The carry bit from this addition is exposed to via a processor status register, but is not normally available to high-level languages.

Some examples:

 2  010      2  010     2   010
 1  001     -1  111     2   010
== ====     == ====    ==  ====
 3  011      1 C001    -4   100

Let's look at those examples individually:

  • 2 + 1 = 3, just like you'd expect. Both the addends and the sum are within the range of positive integers for our word size.
  • 2 - 1 = 1, again just like you'd expect. Internally this operation sets the carry bit, indicating that the addition overflowed the word size. If we were using unsigned numbers, this would be a problem, but with twos-complement numbers it's OK.
  • 2 + 2 = -4, which is definitely not what you'd expect. However, note that the carry bit remains unset. To detect overflow in this case, you'd have to check that two signed inputs resulted in an output with a different sign.

So why isn't overflow checked? The simplest answer is cost.

At the level of the hardware, addition is unsigned (at least on the three processors that I've programmed). Detecting integer overflow would require a separate set of opcodes for signed math, which would mean more transistors, which could be more profitably used elsewhere. In the earlier days of computing that was a huge concern; today, maybe not so much but almost everyone is OK with how math is implemented.

At the level of the language, cost is still a factor. There is the runtime cost of checking every signed operation for overflow, but there is also a programmer cost: imagine having to wrap all expressions (even a for loop) with a try/catch. The .Net runtime apparently gives you the option of enabling this, while Java explicitly does not.

How Twos-Complement Negation Works, and why -MIN_VALUE equals itself

In prose: twos-complement negation flips all of the bits in a number and then adds one.

I use a prose definition because that's almost certainly how it actually works in the hardware (although I'm not a hardware engineer, so can't say for sure, plus different architectures might use different techniques).

Let's see what happens with our 3-bit words:

100 = MIN_VALUE
011 = all bits flipped
100 = after adding 1

Note that there's no carry involved, although you could check for sign of value and result. However, that again would require special circuitry and/or runtime-level checks, to catch a result that will happen almost never.

What Are Some Alternatives, and Why Aren't They Used

One alternative is ones-complement, in which negation is simply inverting all bits. The ones-complement number line for a 3-bit word looks like this:

011 =  3
010 =  2
001 =  1
000 =  0
111 = -0
110 = -1
101 = -2
100 = -3

According to the linked Wikipedia article, there were machines using ones-complement arithmetic; I never used one. Again, I'm not a hardware engineer, but I believe that you need separate operations for addition and subtraction with ones-complement (in addition to separate operations for unsigned math), which again runs into the problem of cost.

The Wikipedia article mentions the problem of "end-around borrow," which may have been an issue with the actual computers that used ones-complement math, but I don't think is a necessary problem. I believe that the carry bit could also serve as a borrow bit.

The bigger problem is that you have two values for zero. Which is going to cause programmers to create a lot of off-by-one errors when counting, or is going to require a lot of special-case code in the language runtime (eg: a for loop that knows when it crosses 0 that it has to skip to 1/-1).

Another alternative is to use the high-order bit just as a sign bit, with the low-order bits being the same between positive and negative:

011 =  3
010 =  2
001 =  1
000 =  0
100 = -0
101 = -1
110 = -2
111 = -3

This is how IEEE-754 floating point works. It makes sense when your primary operations are assumed to be multiplication and division, not so much for addition and subtraction. And it still has the issue of two zeros.

Commentary

To me, this question is identical to questions that express outrage over the fact that 0.10 cannot be represented by a floating point number: both indicate a belief that digital computers should be able to exactly represent the real world. Or, in other words, that computers operate according to the laws of mathematics.

I can understand this belief; what I can't understand is the outrage that people express when the belief is shown to be false. A few moment's reflection should make it apparent that the belief cannot be true: computers work with finite quantities, whereas mathematics deals with continuous relations (I was about to say that everything in the real world is continuous, but figured that someone would bring up quantum mechanics).

Faced with this fundamental truth, computer designers -- and language designers, and application programmers -- have to make trade-offs. You might not like the particular tradeoff, but you should seek to understand it rather than simply complain about it. And once you understand the tradeoff, you can look for an environment that made a different tradeoff.

2 of 3
3

The mathematical reason is that Java implements "arithmetic modulo 2^32". (Or, rather, the CPU implements arithmetic modulo 2^32, and Java exposes the implementation.)

What this means is that, as far as Java's int type goes, numbers that differ by a multiple of 2^32 are considered the same. This means:

  • If a number is too big, you subtract 2^32 until it's not too big.
  • If a number is too small, you add 2^32 until it's not too small.
  • The numbers 2^31 and -2^31 are considered the same, since they differ by 2^32.

Now, Integer.MIN_VALUE is -2^31, so its negation is -(2^31), which is 2^31. However, in arithmetic modulo 2^32, this is considered the same as -2^31, so that's what you get out.

So what are the advantages of arithmetic modulo 2^32? Some of them are...

  • It's easier to implement in hardware than any alternative.
  • It allows applications to use the same instructions for both signed and unsigned arithmetic.
  • It's frequently useful in mathematical applications.

The reason that Java uses arithmetic modulo 2^32 is presumably that Java is simply exposing the way that the CPU implements arithmetic. This is vastly easier and more efficient than any alternative.

🌐
Runestone Academy
runestone.academy › ns › books › published › apcsareview › VariableBasics › minAndMax.html
3.7. Integer Min and Max — AP CSA Java Review - Obsolete
And, Java will return the minimum integer value if you try to add one to the maximum. This is called overflow. It is similar to how odometers work. When would you ever use Integer.MIN_VALUE or Integer.MAX_VALUE? They are handy if you want to initialize a variable to the smallest possible value and then search a sequence of values for a larger value.
🌐
GeeksforGeeks
geeksforgeeks.org › java › min-and-max-in-a-list-in-java
Min and Max in a List in Java - GeeksforGeeks
January 22, 2026 - Handles empty or null lists by returning Integer.MAX_VALUE for min and Integer.MIN_VALUE for max. Time Complexity: O(N log N) and Auxiliary Space: O(N) Java provides built-in methods Collections.min() and Collections.max() to directly find the minimum and maximum values from a collection.
🌐
Android Developers
developer.android.com › api reference › integer
Integer | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Quora
quora.com › What-is-the-minimum-value-of-an-integer-in-Java
What is the minimum value of an integer in Java? - Quora
Answer (1 of 2): A constant holding the maximum value an int can have, 2^31-1. A constant holding the minimum value an int can have, -2^31.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.integer.minvalue
Integer.MinValue Field (Java.Lang) | Microsoft Learn
A constant holding the minimum value an int can have, -2<sup>31</sup>. [Android.Runtime.Register("MIN_VALUE")] public const int MinValue = -2147483648; [<Android.Runtime.Register("MIN_VALUE")>] val mutable MinValue : int · Value = -2147483648 ...
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › Math › min
Java Math min() - Get Minimum Value | Vultr Docs
September 27, 2024 - Consider two identical integer values. Use Math.min() to compare these values.
🌐
Quora
quora.com › How-do-you-calculate-the-average-min-and-max-of-an-int-in-Java
How to calculate the average, min and max of an int in Java - Quora
Answer: original question: How do you calculate the average, min and max of an int in Java? Well, “an int” implies one single integer value. No need to calculate anything, the one int value is all: min, max, average. Otherwise, it depends on how you get your collections of int values: an array ...
🌐
Studytonight
studytonight.com › java-wrapper-class › java-integer-min-method
Java Integer min() Method - Studytonight
The parameters passed are the numeric values by the user to check for the minimum of the two numbers passed. The numerically lower value out of the two values passed as the parameter. Here, one positive and one negative number is taken, hence the negative value is returned and in case of both negative, the value with higher magnitude is returned. import java.lang.Integer; public class StudyTonight { public static void main(String[] args) { int x = 5485; int y = -3242; int z = -5645; // print the smaller number between x and y System.out.println("Lower number is " + Integer.min(x, y)); // print the smaller number between y and z System.out.println("Lower number is " + Integer.min(y, z)); } }