Starting with JDK1.0, the integer defines MIN_VALUE and MAX-VALUE two constants:
/** * A constant holding the minimum value an {@code int} can * has, -2<sup>31</sup>. * * Public StaticFinalintMin_value =0x80000000;/** * A constant holding the maximum value an {@code int} can * has, 2<sup>31</sup>-1. * * Public StaticFinalintMax_value =0x7fffffff;
Q1: Who can explain why these two constants are defined separately as0x80000000And0x7fffffff。
Q2:java.lang.StringWhat is the maximum length?
Q3: Can the following code throw an exception? Why
int x = integer.max_ Value+if (x >= integer.max_value | | x <= integer.min_value) {//throw exception}
-
Q1: What the computer can understand is a binary 01 string 。 The binary representation of numerical values in the computer, with the positive code, the inverse code and the complement. Generally use twos complement to express and operation, min_value = 0x80000000 and max_value = 0x7fffffff is the minimum ( -2^31) and maximum value (2^31-1) of the integer represented by the complement. As for why the use of complementary, simple to say is a convenient operation, the details can be Google or to find the basic teaching materials to double-click. As for the maximum value of the integer, the minimum value is what these two numbers are, because the Java language specification specifies that the int is 4 bytes, whether it is a 32/64-bit machine, which is the underlying part of the cross-platform that it claims to be.
-
q2:string depends on its internal data representation 。 The string inside is represented by a char array, and the length of the array is limited in Java to the maximum value that an int can represent, that is, the max_value = 0x7fffffff . This is indicated by its internal offset int offset and length int count can be reflected.
-
Q3: The code above does not throw an exception, the strategy for exceeding the value of the representation range is the truncation effect, which is to intercept the low and discard the high-level information out of range, which is called overflow. For example, if the operation result of the int type exceeds the representation range, the lower 32 bits (4 bytes in Q1) are intercepted directly as the result of the operation. Thus, the above code 1th line overflow, the result of overflow causes the condition of the 2nd row to judge false.
From for notes (Wiz)
Maximum and minimum values for integers in Java