In

a previous blog, Integer's property of

immutability has been discussed extensively. Basically,

immutability is implemented by some declarations involving with private and final variable and even the way of just exposing a getting interface.

In this blog, we plan to discuss more.
First, let's focus on the amazing implementation of "toString" method. The source code is shown following:
```
1: public static String toString(int i) {
2: if (i == Integer.MIN_VALUE)
3: return "-2147483648";
4: int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
5: char[] buf = new char[size];
6: getChars(i, size, buf);
7: return new String(buf, true);
8: }
```

- The reason for dealing with the special case of Integer.MIN_VALUE is that it does not have the corresponding absolute value. Note that the range of Integer(int) is from $-2^{31}$ to $2^{31}-1$.

Then we go deeper insider to take a look at getChars method. It looks like as following:
```
1: static void getChars(int i, int index, char[] buf) {
2: int q, r;
3: int charPos = index;
4: char sign = 0;
5: // here is why Integer.MIN_VALUE lets getChars fail,
6: if (i < 0) {
7: sign = '-';
8: i = -i;
9: }
10: // Generate two digits per iteration
11: while (i >= 65536) {
12: q = i / 100;
13: r = i - ((q << 6) + (q << 5) + (q << 2));
14: i = q;
15: buf [--charPos] = DigitOnes[r];
16: buf [--charPos] = DigitTens[r];
17: }
18: // Fall thru to fast mode for smaller numbers
19: // assert(i <= 65536, i);
20: for (;;) {
21: q = (i * 52429) >>> (16+3);
22: r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
23: buf [--charPos] = digits [r];
24: i = q;
25: if (i == 0) break;
26: }
27: if (sign != 0) {
28: buf [--charPos] = sign;
29: }
30: }
```

- The first trick used here is at line 13. We show that $100=64+32+4=2^6+2^5+2^2$. Note that if you have to multiply a moderately large number many times, this trick is highly recommended to apply.
- After line 13, it is actually the last two digits of i. Then owing to two pre-defined tables, we are able to get the corresponding digit in constant time. The underlying idea is to cope with two digits at one time. For example, the last two digits are 27. DigitOnes[27] returns 7 and DigitTens[r] returns 2.

```
1: final static char [] DigitTens = {
2: '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
3: '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
4: '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
5: '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
6: '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
7: '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
8: '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
9: '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
10: '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
11: '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
12: } ;
13: final static char [] DigitOnes = {
14: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
15: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
16: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
17: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
18: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
19: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
20: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
21: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
22: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
23: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
24: } ;
```

The number 52429 looks so wired here. In fact, it comes from $\lceil \frac{2^{16+3}}{10}\rceil$. What line 21 aims to do is $q = i * 0.1$.
But as we can see, i is probably 65537 and therefore $65537*52429=3436039373$. Overflow! However, surprisingly, $(65537*52429)>>>19=6553$. Even overflow affects nothing. However again, if we set i as 80000, $(80000*52429)>>>19 \ne 8000$.
I also try to do some potential improvements based on this code. I find we can continue to pull down the line before we stop the phase of deal with two digits at one time. I set is as 1024. Then the line 21 becomes
```
21: q = (i * 3355444) >>> (25);
```

Some simple experiments have been performed and I find this modification indeed improves a little bit.