i++ and ++i in Java

Source: Internet
Author: User

Be familiar with i++ and ++i in C/s + +, then do you know what the following Java code effect is?

One, code example

/** *  * @authorElelule **/ Public classTestplusplusoperator { Public voidtest1 () {inti = 0; I= i++;    System.out.println (i); }     Public voidtest2 () {inti = 0; I= ++i;    System.out.println (i); }}

Second, the output results

01

 

Three, byte code

  Public voidtest1 (); Code:0: Iconst_01: Istore_12: iload_1//Put the I load of unit 1th in the storage stack frame on the operand stack frame 3:iinc 1, 1/ /Add 1 6 to the data in unit 1th in the stack frame: istore_1//Save the data on the action stack frame to unit 1th of the storage stack frame 7:getstatic #2//Field Java/lang/system.out:ljava/io/printstream;10: Iload_111:invokevirtual #3//Method java/io/printstream.println: (I) V14:return  Public voidtest2 (); Code:0: Iconst_01: Istore_12:iinc 1, 1//Add 1 5 to the data in unit 1th in the storage stack frame: iload_1//Will store 1 in the stack frame Number of data in cell load to operand stack frame 6: istore_1//Save data in the stack frame to unit 1th in the storage stack frame 7:getstatic #2//Field Java/lang/system.out:ljava/io/printstream;10: Iload_111:invokevirtual #3//Method java/io/printstream.println: (I) V14:return

Iv. Summary

From the above byte code can be seen,i=i++; This statement was translated into "first move, then the data in the storage unit plus 1, and finally the original data to be moved back", and i=++i; This statement is translated to "Add 1 to the data in the storage unit, then move away, and then move back". is it not the same in C + +? Of course! Can you try these two statements output in C language, or rather?

i++ and ++i in Java

Related Article

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.