Deeply understand the difference between I + + and + + I in Java

Today, let's talk about a misunderstanding about Java. I believe many friends who have just started learning java will encounter this problem. Although the problem is very simple, it is often easy to get confused. Talk about the difference between Java I + + and + + I.

Let's look at the code first:

You can see the result at a glance. What is the result? Is it 10?

I believe there are still many friends who think the answer is 10 at first sight and the correct answer is: 0;

At the beginning of learning C and Java, teachers talked about self increasing forms: I + + and + + I;

In fact, the difference is that I = I + + is assigned first and increases automatically, so no matter how many times the loop is repeated, I on the left is always 0, and the final result is 0 If I = + I is changed, the effect can be achieved, and + + I is self increasing in the assignment first.

It can be understood in this way. Look at the code:

Therefore, to achieve self increment, you can use I = + I, but generally use I + + directly, which is better; This is also a self increasing trap of Java.

The above in-depth understanding of the difference between I + + and + + I in Java is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>