Why use negative int in toString method of integer class in Java SRC for mod operation
When I read the Java version of 1.7 0_ 09 source code, I found that the implementation of toString method of integer class uses negative int to calculate mod operation. Is it meaningful? The code is as follows:
public static String toString(int i,int radix) { if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) radix = 10; /* Use the faster version */ if (radix == 10) { return toString(i); } char buf[] = new char[33]; boolean negative = (i < 0); int charPos = 32; if (!negative) { i = -i; //***** change i to negative } while (i <= -radix) { buf[charPos--] = digits[-(i % radix)]; //***** change back to positive after //***** mod operation i = i / radix; } buf[charPos] = digits[-i]; if (negative) { buf[--charPos] = '-'; } return new String(buf,charPos,(33 - charPos)); }
Solution
According to the algorithm, you need a small stream of (< radius) non negative integers, which fills the character buffer with numbers from right to left Standard primary schools do this by putting a symbol at the beginning of a number and printing out the absolute value of the number But imagine if this rule is that I am always positive in this cycle:
if (negative) { i = -i; // change i to positive }
If I happen to be integer MIN_ Value, then - I also happens to be integer MIN_ VALUE. Two's completion integer variable can store exactly one more negative integer than they can store positive integers However, if the invariant is my always negative absolute value, it will always be suitable for int
Why not use math ABS () or if block? Naturally, in many computer programs, integers are converted to strings very frequently, so it is useful to keep toString as fast as possible The trouble is, math ABS () and if statements may be compiled to use branch instructions when compiled into machine code Branches tend to interfere with instruction pipelining; Therefore, when paying attention to performance, you may choose to remove the if statement from the loop
Note: this optimization is rarely a good idea! Unless your code is called very frequently (such code), or you are building a library with a large number of users and a small number of readers / modifiers (such code) and making the code more difficult to read, understand and change By doing this optimization, Java engineers may speed up the code slightly - but if you use this technology in your code, your colleagues / graders may not be inclined to require stack overflow. Why is your code difficult to understand
The above is the whole content collected and sorted out by the programming house for you on why to use negative int for mod operation in the toString method of integer class in Java Src. I hope this article can help you solve the program development problems encountered in why to use negative int for mod operation in the toString method of integer class in Java Src.
If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.