In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > IT Information >
Share
Shulou(Shulou.com)11/24 Report--
This article comes from Weixin Official Accounts: Low Concurrent Programming (ID: dipingfa), Author: Flash Guest
Note that the language discussed below is Java.
This question has been written badly by many articles on the Internet, but the basic repetition is very high. My feeling after reading it is that most of them are wrong and misleading readers.
Let's open the first one.
Let's come to a conclusion.
i++ is assigned first and then calculated, for example, a=i++, a=i is assigned first and then i=i+1 is calculated, so the result is a==1.
++i is calculated first and then assigned, for example, a=++i, first i=i+1, then a=i, so the result is a==2
And then gave tons of examples to illustrate
public class Test3 {public static void main (String[] args) {int y=0; //Note that "=" is an assignment,"==" is equal//where y=++y is the first operation in the assignment y=++y;/ y==0,++y==y+1; the result y=++y == y+1 == 0+ 1 y=++y;/ y==1,++y==y+1; Fruit y=++y == y+1 == 1+1 ==2y=++y;/ y==2,++y==y+1; Fruit y=++y == y+1 == 2+1 ===3y=++y;/ y==3,++y==y+1; Fruit y=++ y == 3+1 ===4y=++y;/ y==4,++y==y+1; Result y=++y == y+1 == 4+1 ==5System.out.println("y="+y);//5int i =0;// i==0,i++=0; Result i=i++ == (remember to assign first and then calculate)i=i++;System.out.println("i="+i); //0System.out.println ("==============");//1}} First of all, this example is not representative;
Secondly, the conclusions drawn are also extremely misleading;
But the key thing is, it doesn't really help you understand what i++ and ++i really are.
So the difference between i++ and ++i, listen to me.
Forget about "assignment first, arithmetic later"
Don't worry, take your time, hold on till the end
i++ and ++i bytecode view bytecode with javap command, or directly with the idea plug-in, here will not do too much introduction
Write this code in a method
public void ipp() { int i = 1; i++;} View its bytecode
iconst_1istore_1iinc 1 1return Then we write this code
public void ipp() { int i = 1; ++i;} View its bytecode
iconst_1istore_1iinc 1 1return Found no, exactly the same. In other words, when there is no assignment operation, i++ and ++i are compiled into bytecode.
iinc 1 1
exactly the same
How many people understood before that i++ and ++i alone were the difference?
See the definition of iinc bytecode to find the JVM official manual
https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.iinc
See that the format of the iinc bytecode instruction is
iinc index const
index indicates the index of the local variable table, const indicates how much to add to its value
So the top
iinc 1 1
said
Add 1 to the value at position 1 of the local variable table index
The value at index 0 of the local variable table is this, and the value at index 1 is set by int i = 1, which is 1. Add 1 to this value, and it becomes 2.
Let's review the code above.
public void ipp() { int i = 1; i++; System.out.println(i);}public void ppi() { int i = 1; ++i; System.out.println(i);} If you print the value of i, it is easy to know that both are 2
So very simple, i++ and ++i itself in the bytecode instructions are reflected in iinc, that is, simply put i in the local variable table that position value,+1
So let's make this a little bit more complicated, and we'll do the ++ operation, and we'll assign it to i.
public void ipp() { int i = 1; i = i++;}public void ppi() { int i = 1; i = ++i;} Guess what the value of i is
Don't worry, check the bytecode again
void ipp() --> i = i++;
iconst_1istore_1iload_1iinc 1 1istore_1returnvoid ppi() --> i = ++i;
iconst_1istore_1iinc 1 1iload_1istore_1return This time you see it differently, but the bytecode instructions are the same, just in a different order
i = i++ is iload_1 then iinc 1 1
i = ++i is iinc 1 1 then iload_1
So it's also quite simple that i++ and ++i differ only in their final assignment to a variable (actually because they participate in an operation, since direct assignment is also an operation without an operand), and only in their order.
What would happen if the order was different? Let's refine the process by looking at the VM stack
Observe the changes in the virtual machine stack, but you have to know what the virtual machine stack is first, and you have to know the memory partition of the JVM. This block will not help you review it. After the ipp () method is directly put into the virtual machine stack, the initial structure in the method stack frame.
See i = i++
The bytecode for the ipp () method is executed step by step
iconst_1istore_1iload_1iinc 1 1istore_1return Note that this is represented at 0 in the local variable table. It is not written here for simplicity, and then the "frame" of the stack frame is written incorrectly. I will not change it arbitrarily. Ha
iconst_1: Stack immediate number 1
istore_1: operand stack top-> local variable table 1 position
iload_1: Local variable table 1 position-> operand stack top
iinc 1 1: local variable table 1 position value +1
istore_1: operand stack top-> local variable table 1 position
So, the last value of i, which is the value at position 1 in the local variable table, is 1.
Let's animate it again.
You can feel that i = i++, iinc 1 1 is useless because the value at position 1 of the last local variable is overwritten by the value at the top of the operand stack in the last assignment, so +1 is useless.
So idea will also prompt you that the i++ here is useless
the value changed at 'i++' is never used
See also i = ++i
Believe me, you can deduce that yourself.
iconst_1istore_1iinc 1 1iload_1istore_1returniconst_1: Stack immediate number 1
istore_1: top of operation stack-> local variable table 1 position
iinc 1 1: local variable table 1 position value +1
iload_1: Local variables table 1 position-> top of operation stack
istore_1: top of operation stack-> local variable table 1 position
So, the last value of i, which is the value at position 1 in the local variable table, is 2.
Let's just animate it
Essential difference. So see what the essential difference is?
difference is
Yes "+1 the value in the local variable table before putting it on the operand stack"
Or "put it on the operand stack first, and then +1 the value in the local variable table"
that's all
So the common saying on the Internet, i++ means to assign first and then calculate
Assignment is pushing the top of the operand stack.
The operation is the local variable table +1 operation
"I don't know."
There is also a saying that i++ is to use i first and then + 1;
There is also a saying that i++ is assigned first and then increases.
There is no doubt about it. ...
Brother, let's not mislead readers with our own words, okay?
So in conclusion, in my words, there is no ambiguity.
i++: First put i in the local variable table into the operand stack, and then add i value in the local variable table +1.
++i: +1 the value of i in the local variable table, and then put i in the operand stack
After you understand it from this angle, you can do similar complicated problems again. At most, you can deduce them from scratch in your mind
Look at the question.
int i = 2;int y = i++ + ++i;y = ? int a = 2;a = a++ + ++a;a = ? int b = 2;b = b++ + (++b + ++b) + (b += 2);b = ? si
test
a
points
Zhong
answer
y = 6
a = 6
b = 18
Did you do it right?
I'll show you the byte code for the hardest one.
According to the yellow words, you can change to the operand stack (from left to right, the operand stack from the bottom of the stack to the top of the stack). Make up the animation in your own mind. I don't want to be lazy. Hahaha ~
int b = 2;b = b++ + (++b + ++b) + (b += 2);iconst_2 ; operand stack 2istore_1 ; Local variable table b=2iload_1 ; operand stack 2iinc 1 by 1 ; local variable table b=3iinc 1 by 1 ; local variable table b=4iload_1 ; operand stack 2 4iinc 1 by 1 ; local variable table b=5iload_1 ; operand stack 2 4 5iadd ; operand stack 2 9 (=4+5) iadd ; operand stack 11 (=2+9) iinc 1 by 2 ; local variable table b=7iload_1 ; operand stack 11 7iadd ; operand stack 18 (=11+7) istore_1 ; Local variable table b=18 No matter how difficult it is, I think it's a little boring. Let's give ourselves a question ~
If you are confused about the order of the stack here, for example, you feel that adding () is not mathematically the first operation? Why didn't he push the stack first to participate in the calculation?
In fact, this is irrelevant to the knowledge of i++ and ++i. What you need to know is the prefix, infix and suffix expressions. Here, just an example will not be explained.
Simply put, it is how to convert mathematical expressions into a format. According to this order, it is convenient to realize calculations through stacks.
such as
b++ + (++b + ++b) + (b += 2)
The ++ operation is not affected at all in the process of converting to suffix expression, and it is simplified to
b + (b + b) + b
Converted to a suffix expression
b b b + + b +
Stack pressing in this order, that is, the order of instructions in bytecode, for example, b stack pressing is iload_1, operator (+) stack pressing is iadd, you go back and prove it ~
And every value of b here is the value of b at that time of stack pushing.
So, the ++ question on the Internet is actually two knowledge points.
Bytecode instructions when i++ and ++i participate in operations
Convert mathematical expressions to suffix expressions for stack operations
Most of the explanations on the Internet are not from the most direct byte code instructions, but also confuse the two knowledge points, which I think is irresponsible.
Go back to the conclusion of the article at the beginning
i++ is assigned first to the operation, for example, a=i++, a=i is assigned first, and then i=i+1 is operated, so the result is a==1
++i first evaluates, for example, a=++i, first evaluates i=i+1, then assigns a=i, so the result is a==2
Not to mention that it does not use bytecode to explain the problem, have you found that this is wrong in itself, there is a lot of misleading.
First assign a=i, then calculate i=i+1
In fact, there is no assignment at all, just throw i into the operand stack waiting to be operated on, then the local variable table i=i+1, and finally the i in the operand stack is popped and written to the position of a in the local variable table, then it is called assignment.
In short, it is better to read less articles like this
This article comes from Weixin Official Accounts: Low Concurrent Programming (ID: dipingfa), Author: Flash Guest
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.