I tried to execute this piece of java code in NetBeans but as I debug it step by step , whenever I bring the cursor of the mouse inside it to check the value of 'a' variable , It will be incremented strangely and the output shows the wrong answer. What is the problem?
Asked
Active
Viewed 58 times
-1
-
Using netbeans 11.2 and openjdk 11 debug shows a value of 10. Which is correct. Update netbeans maybe. If that is not the case, update jdk. – Just_Alex Feb 03 '20 at 08:31
-
I mean whenever I debug this I will get different answer in output . the output is not constant . Once I debug it and I got 42 in output. How is it possible? – Yasin Fakhar Feb 03 '20 at 09:08
-
I'm not sure how you can get 42 in the output print of the console. If you are referring to the debug pop-up it is not meant to be reliable, but in netbeans-version 11.2 I found the pop-up 'a= 10'. – Just_Alex Feb 26 '20 at 02:52
1 Answers
0
The post increment (a++) evaluates the value and after that it will be incremented. The pre increment (++a) increments the value and after that it gets evaluated. So, if you use a=1 and you go a++ + +aa you will have evaluated on that line a=3 and on the next line it will be a=4.

Dan Serb
- 145
- 10
-
No , You did not get the picture . I mean whenever I debug this I will get different answer in output . the output is not constant . Once I debug it and I got 42 in output. How is it possible? – Yasin Fakhar Feb 03 '20 at 09:06
-
Is that the only code you have? Do you have a warning on 'a', or why is it in yellow? – Dan Serb Feb 03 '20 at 09:07
-
Yes , that is the only code and no warning . The color is Yellow because I drag it to check the value – Yasin Fakhar Feb 03 '20 at 09:13