I have a little doubt here,i have a code as
int num=0;
for(int i=0;i<5;i++){
num=num++;
System.out.print(num);
}
why is the output always 00000
The ++ operator increments num last, so when num is 0, you are setting it to 0 again.
It has to deal with how the ++ operator increments num, and what num is truly pointing to. To avoid it, just use num++
Interestingly enough, num=++num will correctly increment and assign the incremented value, though the whole purpose of the ++ operator, either pre or post, is that it modifies the value directly. You do not have to re-assign it back to the value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With