Ask your own question, for FREE!
Computer Science 28 Online
OpenStudy (anonymous):

Public class Increment { public static void main (String[] args) { int j=0; for (int i=0 ;i <100 ; i++ ) j=j++ ; System.out.println (j); } } what will be output ? /*Assume their is no compilation error*/

OpenStudy (e.mccormick):

Well, have you worked through the order things will happen in?

OpenStudy (anonymous):

output is 0.... i need explanation....

OpenStudy (e.mccormick):

Well, first, do you know what is being looped? Then second, do you understand the difference between j++ and ++j?

OpenStudy (e.mccormick):

Oh, and I forgot to add, the order for how = is done. That is one of the other issues here.

OpenStudy (anonymous):

yes @e.mccormick

OpenStudy (e.mccormick):

If you understand how those work, that is why the result is 0 and only 0.

OpenStudy (anonymous):

are you explaining or questioning,,,,,?

OpenStudy (e.mccormick):

Look at these differences: ``` Public class Increment { public static void main (String[] args) { int j=0; for (int i=0 ;i <100 ; i++ ) j=j++ ; System.out.println (j); } } ``` output: 0 ``` Public class Increment { public static void main (String[] args) { int j=0; for (int i=0 ;i <100 ; i++ ){ j=j++ ; System.out.println (j); } } } ``` output: 0, 100 times ``` Public class Increment { public static void main (String[] args) { int j=0; for (int i=0 ;i <100 ; i++ ) j=++j ; System.out.println (j); } } ``` output: 99

OpenStudy (anonymous):

sorry you are wrong for "++j" output is 100

OpenStudy (e.mccormick):

True. 99 would be the 100th event for i, so ++j would happen 100 times. However, do you understand why the small code changes get the different results? It has to do with the mathematical idea of order of operations as applied in computer programming. This is called operator precedence. . I asked if you understood the difference between j++ and ++j, then added if you understood how = works. You said you did, but still seem confused. So I am not sure if you understand how the operator precedence for these works or not.

OpenStudy (e.mccormick):

I have been gone the last 5 hours and do not see a reply. Well, here is the logic: j=j++ ; = Assignment operator. Assign the results of the right side to the left. Order of operations is right to left. j++ post-increment of j. ++, in this use, does effectivly the same as j = j+1 but only after j has been used. Order of operations is left to right. What happens is basically this: 1) j = is seen by the compiler, and a temp variable is created. 2) j++ is seen as the entire right side of the operation. It is evaluated to be assigned to the temp variable. 3) Because j++ does not increment until after use, the temp variable is set to 0 4) The ++ evaluates and j becomes 1. 5) j= evaluates and is assigned the value of the temp variable, which is 0. This is repeated 100 times (basically the same as j=0 100 times) then printed once. If you had a debugger in sub-line step debugging mode you should be able to watch j toggle between 0 and 1 as it goes through that one line.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!