Search code examples
javaprimitive

How do Integer.MAX_VALUE and Integer.MIN_VALUE get initialized?


This was a stupid question. I was still learning and was drastically overthinking the situation.


In the source code for Integer, it shows MIN_VALUE and MAX_VALUE declared as:

public static final int MIN_VALUE = 0x80000000;  
public static final int MAX_VALUE = 0x7fffffff;

My question is, how do these ints get declared in the first place? It seems as though these values would have to be known in order for the values to be validated (or overflow, or whatever) to begin with. It obviously can't check itself before it's declared, so how does this work?


Solution

  • how do these ints get declared in the first place?

    You are showing the two lines where they are declared...

    it seems as though these values would have to be known in order for the values to be validated

    These constants are known and these are their values.

    In other words, the limit for an int value is constrained by the fact that they have to fit in 32 bits. Those 2 variables are there as a convenience but are not used to determine whether an int should overflow or not.

    The range of int values is defined in the Java Language Specification #4.2.1 - these constants only reflect the specification:

    The values of the integral types are integers in the following ranges:

    • [...]
    • For int, from -2147483648 to 2147483647, inclusive