Regain the i++ and ++i in Java

Source: Internet
Author: User
i++ and ++i in Java

First remember the main points:

1.i++ is the first value and then the operation.

2.++i is the value after the first operation.

Give me a chestnut:

int y,x=3;y= (++X) + (++x); Then y=?

Analysis: First calculation after the value, first calculate ++x, get the result x=4, then calculate the second ++x,x becomes 5, at this time the first X is 4.

So the last x=5,y=9.

Again, int y,x=3;y= (x + +) + (x + +); Then y=?

Analysis: First take the value after the operation, first take the value x=3, then calculate the first x + +, get x=4, then take the second x=4, and then calculate the second x++,x into 5.

So the last x=5,y=7.

Again, int y,x=3;y= (x + +) + (++x); Then y=?

Analysis: The first value after the operation, first take the value x=3, and then calculate the first x + +, get x=4, and then calculate the second x++,x into 5.

So the last x=5,y=8.

Summary: The results are in the jdk1.8 environment to run the results, in the previous Baidu to the answer is wrong, but provided an analysis of ideas, now according to the above thinking analysis is no fault, so the practice of genuine knowledge (O゜▽゜) o☆[bingo!]

Regain the i++ and ++i in Java

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.