i++ problems in Java

Source: Internet
Author: User

Have trouble solving an interview problem.

 Public class INC {    publicstaticvoid  main (String args[]) {        new Inc ();         int i =0;        Inc.fermin (i);        I=i++;        System.out.println (i);    }     void Fermin (int  i) {        i+ +;    }}

The answer to this question is output 0.

Check the relevant information, only know i=i++ this step, the real implementation is temp = i;i=i+1;i=temp;

Not verified that's true, I changed the code to read as follows:

 Public class INC {    publicstaticvoid  main (String args[]) {        new Inc ();         int i =0;        Inc.fermin (i);         int j=i++;        System.out.println (j);    }     void Fermin (int  i) {        i+ +;    }}

The output is still 0, which is in line with expectations. namely:temp=i;i=i+1;j=temp;

  Rather than just doing the i++ step like some C + + +.

  In fact, i=i++, or j=i++ after the implementation of i++ this step, I the value will be increased, but I (/j) =temp This step to change the I value back, so long as the code is changed as follows so you can see the change of I value :

 Public class INC {    publicstaticvoid  main (String args[]) {        new Inc ();         int i =0;        Inc.fermin (i);         int j=i++;        System.out.println (i);    }     void Fermin (int  i) {        i+ +;    }}

The result of the output is 1.

In the final analysis is still a compiler problem, there is no need to overly tangled in this!

  

 

i++ problems 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.