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 the opposite.


In fact, in fact, at every step we use the rules we said above, where a++ + ++a equals 8. is because 3+5 equals 8, the first time I encounter a++ a value of 3, but after the run play value becomes 4, and after the ++a operation becomes 5

But look at the following procedure:


Due to the addition of a space in the middle, is understood in order to add, so now is 3+4 equals 7


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



In fact, it's 4+5=9, because it's ++a. So we'll start with + + and wait until the second ++a is calculated. A is already 4, so the answer is 9.

Look at a more complex example:


The following is an analysis:



Summary: So basically still master a++ is first to participate and calculate again ++,++a is the first + + to participate in and calculate, only a value change is not a formula to be done before the change. But every step of the calculation is likely to change.

This shows that the operation of the + + to the ordinary operation of the readability will be very poor, in addition, due to the impact of added. It is possible to accidentally make a result completely, so try not to let the + + operation into too complicated operations, can choose to replace him in a 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.