Results of the operation of i = i++ in Java

Source: Internet
Author: User

The result of the operation of i = i++ is indeterminate, but in Java, the result is deterministic.

int i = 5;i = i++; System.out.println (i);

Output Result: 5

int i = 5;i = ++i; System.out.println (i);

Output Result: 6

With Javap disassembly, the assembly code is as follows:

0:ICONST_5//constant 5      1:istore_1//assignment assigns constant 5 to I 2:iload_1//       read 3:iinc      1, 1//self plus 16:istore_1      //Assignment Assign the value read in step 2 to I

0:iconst_5      1:istore_1      2:iinc      1, 15:iload_1       6:istore_1

After reading the disassembly, look at the following code, the result is very easy to understand, the result is 12

int i = 5;i = i++ + ++i; System.out.println (i);

I = i++ results 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.