The original link: https://blog.csdn.net/android_cai_niao/article/details/106027313
# preface
I believe that many friends may have baidu before i++ related articles and + + I, also made a lot of practice, feel oneself have deeply master the principle between them, is it true? Try to calculate the I offer the following a few exercises, you will find that you will not again!
Sample code #
Please mental arithmetic about his own answer first, and then down to find a book, and then answer behind compared with me, look for a few questions, you can do can do for more than two questions I shout your brother!!!!!
# # example 1
int i=0;
I=i++;
System. The out. Println (" I="+ I);
# # sample 2
Int a=2;
Int b=(x + 3) + a;
System. The out. Println (b);
# # sample 3
Int a=2;
Int b=a + + (3 *);
System. The out. Println (b);
Example # # 4
int i=1;
Int j=1;
Int k=i++ + + + I + + + j + j++;
System. The out. Println (k);
# # example 5
Int a=0;
Int b=0;
A=+;
B=+;
System. The out. Println (" a="+ a +", b="+ b);
# sample answer
The sample's 1-0
Sample 2:9
Example:
Sample 4:8
Example 5: a=1, b=0
Principle # i++ and + + I
I++ namely after gagarin, principle is: the first, and then return on the previous value
+ + I namely before gagarin, principle is: the first, and then return after increasing the value of the
Key: this is the average person did not know, remember: whether it is a + + + +, after or before all have in common is on the first,
For + + I do not say, most people have to understand, for the principle of i++, I use the code to simulate the principle, the following:
Int temp=I;
i=i + 1;
return temp;
The three other code is what is said above: i++ is on the first, and then return to the previous value,
# # i++ bytecode analysis
There are lots of people are written on the paper said i++ is to return the value of the I first, then the increase, this is wrong, is on the first, and then return to the previous value, you may ask, is there any difference between this? Answer: yes, as long as this didn't understand, you when calculating the i++ related question could be wrong,
Some people may ask, what makes me believe you, do you have any evidence to prove that i++ is on the first, and then before returning to the increase of value? I have to find out the evidence, we get the class bytecode out, analyze, prove the following:
Public class Test {
Void fun () {
int i=0;
I=i++;
}
}
As above, we wrote a super simple Test class, enter this command in the CMD (javap -c Test. The class) to see the generated bytecode, screenshots are as follows:
We focus on fun () method, this passage is as follows:
This is fun () function corresponding to the bytecode, our line to analysis, first of all, we want to say two concepts, one is a variable, a stack is operating, fun () method, there are two variables, alas, is not only a variable I? How can have two? To understand the you need to learn the related knowledge of byte code, we don't get here, my drawing is as follows:
As shown above, the variable has two, what is at position 0 variables we don't tube, the system automatically assigned, you want to know what is the position 1 variables is actually we define the variable I went, then, we analysis to asm fun () method of the corresponding byte code:
"Iconst_0" I on behalf of type int, const represent constants, 0 means integer 0, the meaning of the sentence is put int constant 0 in the operation of the stack stack, the diagram is as follows:
"Istore_1" I on behalf of type int, storage store representative, on behalf of the position for 1 variables, the meaning of the sentence is the operating values on the top of the stack of stacks away and saved to the position for 1 on the variables of diagram is as follows:
"Iload_1" I on behalf of type int, the load on behalf of the value of the variable load, on behalf of the position for 1 variables, the meaning of the sentence is to keep the value of the variable loading position is 1 to the operation of the stack stack, diagram is as follows:
Iinc 1, 1, "" I on behalf of type int, inc (increment) on behalf of the increase, there are two 1, the representative to the position for 1 in front of the variable, the second one represents 1, because of I +=3 this since the operation, the case Numbers would be 3, 2 or 3 (iinc 1, 3) on the," iinc 1, 1, "the meaning of the sentence the position for 1 1, the value of the variable diagram is as follows:
Note: since the add operation does not change the values in the stack operation, so the value of the variable I since increased into 1, while operating in the stack the value is 0,
"Istore_1" I on behalf of type int, storage store representative, representative position 1 of variables, the meaning of the sentence is: the values in the stack and saved to the location of 1 variable, diagram is as follows:
* * so, these lines bytecode together, i++ is not the first, and then before returning to the increased value!! So you don't make wrong order, * * with code understand, is equivalent to the code below:
Int temp=I;
i=i + 1;
return temp;
Finally, stick + + I bytecode chart also, you can according to my knowledge the interpretation of the above analysis, will know the difference between a + + I and i++ :
Void fun () {
int i=0;
I=+ + I;
}
Principle # expression
Expressions have a principle: a variable is expression, a number of expressions of addition and subtraction operations are carried out from left to right
Look at the if statement of one of the structure definition:
If (conditional expression) statement;
Write code in this structure, as follows:
Boolean b=true;
int i=0;
If (b) i++;
According to the definition of the structure of the if statement above, if the brackets is an expression, but the code above wrote in one variable b, this is a variable, how can also as an expression? That's right, a variable is expression,
* * remember the key: a variable is expression, a number of expressions of addition and subtraction operations are carried out from left to right * *
At this point, it is estimated that there will be someone to the order of operations and multiplication of these mixed up, the sample is as follows:
Int a=0;
Int b=a + a * 2;
As above code, according to my view, a variable is an expression, "a + b=a * 2" here a appeared twice, is there are two a expression, from left to right words to calculate a + a, this is definitely not ah, this is not what I meant,, method of priority or not disorderly, it should be a * 2? Also is wrong, should be like this: because of the multiplication, so a * 2 priority of expression, rather than a + a form expression, that is to say, in general can be divided into two expressions: "a" expression and the expression of "a * 2", the two expressions together must be calculated from left to right, finished with a first expression results, and then calculate a * 2 expression as a result, you may want to calculate a and to calculate a * 2 is there a difference? The answer is: yes, read the following "sample 3 explanation" was clear to you,
# sample answers,
# # example 1,
int i=0;
I=i++;
System. The out. Println (" I="+ I);//the result: 0
See i++ first, according to the principle of "on the first, and then before returning to the increase of value", I after the increase, I=1, but then before returning to the increased value of 0, the expression into I=0, 0 not assigned to the I I value is 1, but when give I 0 assignment, I value and then become 0, thus I=i++ the code is doing this, because I value ultimately, as the original,
# # example 2,
Int a=2;
Int b=(x + 3) + a;
System. The out. Println (b);//result: 9
Int b=(x + 3) + a; + after, a=3, and return on the previous value 2, so this time expression is:
Int b=(3 * 2) + a; At this time a value is 3, expression becomes again:
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull