For I 0 I 5 i if I 10 continuesystemoutprintI

int i = 5; i = i++; System.out.println(i); it prints 5; please explain.

807601

Hello all,

I am planning to go for SCJP can you please explain this:

inside main() i have this.

int i = 5;
i = i++;
System.out.println(i);

//it prints 5; please explain.

Thanx.

Comments

  • masijade

    masijade Member Posts: 6,715 Blue Ribbon

    You see that little text field on the left side of the browser with the words "Search Forums" above it? Type i = i++ into that window and press the button ">>" right next to it. This question has been answered literally thousands of times.

  • 807601

    I am really sorry. I should have first searched for it.

  • 807601

    hai even though u increment "i" it will not affect immediately.the result will be 5 only.to see the difference u make it in a "for" loop,compile and run.then u can c the difference.

  • 807601

    C has nothing to do with this. Neither has U (whatever U may be, I am sure it has nothing to do with this).
    In fact in C the behaviour of the statement is undefined while in Java is IS defined, and defined to be as observed.

  • 807601

    Dheeraj_Gaba wrote:
    can you please explain this:
    i = i++

    Sure. What we have here is an assignment.
    The syntax of an assignment is variable = expression.
    The semantics of an assigment is: evaluate the expression, store the resulting value in the variable as a side-effect and return the stored value as a result of the assignment.

    So first we have to evaluate the expression. In your code this is a postfix increment of the form variable++. The semantics of such an expression is:
    1. memorize the value of the variable (which is 5)
    2. increment the variable by one (i is now 6) - this is another side-effect.
    3. return the memorized value from step 1 (5) as the value of the expression, NOT THE CURRENT VALUE OF THE VARIABLE!

    After we have have evaluated the expression to the value 5, we now store the resulting value in the variable i.

    So basically, i starts at 5, then gets incremented to 6 by the side-effect of the postfix-operator, and then returns to 5 as a side-effect of the assignment. The following semicolon turns the assignment expression into a statement, and the value 5 is discarded.

  • 3004

    3004 Member Posts: 204,171 Green Ribbon

    Sudigali wrote:
    hai even though u increment "i" it will not affect immediately.the result will be 5 only.to see the difference u make it in a "for" loop,compile and run.then u can c the difference.

    Absolute nonsense.

    It's too bad that the person asking the question--the least likely one to know if an answer is correct--is able to mark an answer as correct.

  • 807601

    jverd wrote:
    It's too bad that the person asking the question--the least likely one to know if an answer is correct--is able to mark an answer as correct.

    I don't even understand that "correct" answer, lol.

  • 807601

    I've asked it before, and I'll ask it again:

    Why do so many people get this wrong? Don't they think to question the fact that there's apparently a short-cut that isn't actually any shorter, and actually requires more keystrokes?

  • 807601

    Thank you very much for all the replies though I think I should have taken a bit more time to mark the correct answer.

    Edited by: Dheeraj_Gaba on May 21, 2008 11:34 AM

  • 807601

    georgemc wrote:
    I've asked it before, and I'll ask it again:

    Why do so many people get this wrong? Don't they think to question the fact that there's apparently a short-cut that isn't actually any shorter, and actually requires more keystrokes?

    probably because they read Herb Schildt's books instead of the JLS.

This discussion has been closed.

hefnerclact1984.blogspot.com

Source: https://community.oracle.com/tech/developers/discussion/1191039/int-i-5-i-i-system-out-println-i-it-prints-5-please-explain

0 Response to "For I 0 I 5 i if I 10 continuesystemoutprintI"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel