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