Understanding of i++ problems in Java
Interview problem Analysis
In the Tuesday interview, encountered a question of the pen test, here to detailed analysis, find out the cause of the problem, solve confusion, to avoid the same problems.
Here's the code: for Loop traversal 20 times, copy the value of count++ to count, and ask what the result of the last count is?
public static Span class= "Hljs-keyword" >void main (string[] args) throws Exception {int count = 0 ; for (int i = 0 ; i < 10 ; i++) {count = count + +; //count= ++count; //count=count++ + ++count; //Count=++count + count++; System.out.println ( "count==" + count ); } System.out.println (count ); }
Difficulties and doubts
This problem is mainly to examine how count=count++ in the Java Virtual machine is executed, its sequencing.
In Java, count=count++, put the value of Count first, then execute count++, then copy the previously stored value to count (instead of copying the value after count+1 to count), so what we see is that no matter how the value is assigned, The value of Count is always the original default value. If it is in the C language, I guess the answer will be different, according to a classmate tested, is not the same, I did not test.
Extended content
When we adjust the count=count++ to Count=++count, then the content will be how much, the result, will be so-called 1,2,3,4 ... such as
The procedure is: count yourself plus 1, and then assign the self-added result to count, so it won't stay the same.
Then we are discussing, count=count++ replaced with count=count++ + ++count, so what will be the result?
In this case, the execution of Count is performed by first executing the rightmost count+1, assuming that the initial value of Count is 0, then the rightmost 1 is added, the result becomes, count=1, and then executes Count=count+count, At this time, the contents of the count itself has been 0 to 1, and the count value of the + number is 1, so that the two parts add the result becomes 2, the first result is indeed 2.
What happens when we change the count=count++ to Count=++count + count++?
The result is the same as above, the first result is still 2, its execution order is: first execute the assignment = number next to the ++count, that is, let count himself plus 1, so that the contents of count from 0 to 1, and then two 1 add, is called 2.
This is what I want to say, count=count++; it may be questioned why the Count suffix plus 1 is not assigned to count, and the prefix plus 1, after modifying the contents of count, can be assigned to count. In fact, or in front of the bold text said, memory will be temporarily stored in the previous, in count=count++, after the JVM analysis, the Count suffix plus 1, the original count value will be recorded, stored in a storage space, assigned to count, Count himself added 1. and the prefix plus 1 of the highest priority, the first directly modified the contents of the count, JVM parsing, semantic analysis, the content of the count is assigned to count. Through such tests, it can be explained that the process, after all, the final test results, the comparison of multiple sets of data, also should be proven my guess, of course, more in-depth, but also to see how the JVM analysis, processing.
Extension of another question
Here is a topic, similar to the above:
publicclass MyTest { static{ int x=5; } staticint x,y; publicstaticvoidmain(String[] args) { x--; myMethod(); System.out.println(x+ y++ + x); } privatestaticvoidmyMethod() { // TODO Auto-generated method stub y= x++ + ++x; }}
The result is 2, the reason why, we all analyzed.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Understanding of i++ problems in Java