a++ and ++a in Java are analyzed in more complex operations

Source: Internet
Author: User

About Lianga and Lian-minus: http://blog.csdn.net/ieayoio/article/details/46431843

Here's a code that's universally understood:

public static void Main (string[] args) {int a=3; SYSTEM.OUT.PRINTLN (a++ + 1);//4system.out.println (a);//4system.out.println (++a + 1);//6}

a++ is calculated first and then + +, and ++a opposite


In fact, in fact, in every step of the use of the rules we said above, here a++ + ++a equals 8, is because 3+5 equals 8, the first encounter a++ when the value of a is 3, but the execution after playing the value becomes 4, and after the operation of ++a becomes 5

But look at the following procedure:


Because there is a space in the middle, is understood in order to add, so now is 3+4 equals 7


If the above understanding, the following piece of code is very easy:



Actually is 4+5=9, because is ++a, therefore will first + +, waits for the computation to the second ++a, a already is 4, so the answer is 9

Let's look at a more complicated example:


Here is the analysis:



Summary: So basically still master a++ is first to participate in the calculation and then ++,++a is the first + + to participate in the calculation, but the value of a change is not a formula after the change, but every step after the calculation is likely to change

It is obvious that + + operation to participate in the ordinary operation of the readability will be very poor, in addition, because the impact of the added, may inadvertently is completely to make a result, so try not to let + + operations involved in too complex operations, can choose to replace him in the way












a++ and ++a in Java are analyzed in more complex operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.