Q about increment and assignment

Discussion in 'Java' started by Big D, Dec 23, 2006.

  1. Big D

    Big D Guest

    I'm confused by the output of the following code:

    public class PP {
    public static void main(String[] args) {
    int i = 1;
    i = i++;
    System.out.println(i);
    }
    }

    It outputs 1.

    I understand the assignment operator happening before the ++, but I don't
    understand why the ++ doesn't increment. I thought the statement should
    basically expand to:

    i = i;
    i = i+1;

    but the ++ gets lots somewhere...
     
    Big D, Dec 23, 2006
    #1
    1. Advertising

  2. Big D wrote:
    > I'm confused by the output of the following code:
    >
    > public class PP {
    > public static void main(String[] args) {
    > int i = 1;
    > i = i++;
    > System.out.println(i);
    > }
    > }
    >
    > It outputs 1.
    >
    > I understand the assignment operator happening before the ++, but I don't
    > understand why the ++ doesn't increment. I thought the statement should
    > basically expand to:
    >
    > i = i;
    > i = i+1;
    >
    > but the ++ gets lots somewhere...


    No, it gets expanded into:

    t = i;
    i = i + 1;
    i = t;

    Don' do dat!

    --
    John W. Kennedy
    "The blind rulers of Logres
    Nourished the land on a fallacy of rational virtue."
    -- Charles Williams. "Taliessin through Logres: Prelude"
     
    John W. Kennedy, Dec 23, 2006
    #2
    1. Advertising

  3. Big D wrote:
    > I'm confused by the output of the following code:
    >
    > public class PP {
    > public static void main(String[] args) {
    > int i = 1;
    > i = i++;
    > System.out.println(i);
    > }
    > }
    >
    > It outputs 1.
    >
    > I understand the assignment operator happening before the ++, but I don't
    > understand why the ++ doesn't increment. I thought the statement should
    > basically expand to:
    >
    > i = i;
    > i = i+1;
    >
    > but the ++ gets lots somewhere...
    >


    In Java, any side effects of operand evaluation happen before side
    effects of the operation that uses the operands. i++ will be completely
    evaluated before the assignment of its value to i.

    The value of "i++" is, by definition, the value of i prior to the
    increment, so the assignment restores the old value of i. The code is
    equivalent to:

    int temp = i; // record the value of i++, the old value of i
    i = i+1; // increment i
    i = temp; // assign the value of i++ to i.

    Patricia
     
    Patricia Shanahan, Dec 23, 2006
    #3
  4. Big D

    Karl Uppiano Guest

    "John W. Kennedy" <> wrote in message
    news:c0ijh.1967$...
    > Big D wrote:
    >> I'm confused by the output of the following code:
    >>
    >> public class PP {
    >> public static void main(String[] args) {
    >> int i = 1;
    >> i = i++;
    >> System.out.println(i);
    >> }
    >> }
    >>
    >> It outputs 1.
    >>
    >> I understand the assignment operator happening before the ++, but I don't
    >> understand why the ++ doesn't increment. I thought the statement should
    >> basically expand to:
    >>
    >> i = i;
    >> i = i+1;
    >>
    >> but the ++ gets lots somewhere...

    >
    > No, it gets expanded into:
    >
    > t = i;
    > i = i + 1;
    > i = t;
    >
    > Don' do dat!
    >
    > --
    > John W. Kennedy


    Holy crap! I hope no one ever asks me to evaluate code like that on a job
    interview. I would never write code like that, but I doubt if I'd squirt
    that nuance from the top of my head.
     
    Karl Uppiano, Dec 24, 2006
    #4
  5. Karl Uppiano wrote:
    > "John W. Kennedy" <> wrote in message
    > news:c0ijh.1967$...
    >> Big D wrote:
    >>> I'm confused by the output of the following code:
    >>>
    >>> public class PP {
    >>> public static void main(String[] args) {
    >>> int i = 1;
    >>> i = i++;
    >>> System.out.println(i);
    >>> }
    >>> }
    >>>
    >>> It outputs 1.
    >>>
    >>> I understand the assignment operator happening before the ++, but I don't
    >>> understand why the ++ doesn't increment. I thought the statement should
    >>> basically expand to:
    >>>
    >>> i = i;
    >>> i = i+1;
    >>>
    >>> but the ++ gets lots somewhere...

    >> No, it gets expanded into:
    >>
    >> t = i;
    >> i = i + 1;
    >> i = t;
    >>
    >> Don' do dat!
    >>
    >> --
    >> John W. Kennedy

    >
    > Holy crap! I hope no one ever asks me to evaluate code like that on a job
    > interview. I would never write code like that, but I doubt if I'd squirt
    > that nuance from the top of my head.


    Java is an improvement over its ancestor, C. In C, the official
    interpretation of the code in question is "whatever the compiler feels
    like doing". Because Java places such an emphasis on portability, it
    sets down rules.

    --
    John W. Kennedy
    "The blind rulers of Logres
    Nourished the land on a fallacy of rational virtue."
    -- Charles Williams. "Taliessin through Logres: Prelude"
     
    John W. Kennedy, Dec 24, 2006
    #5
  6. Karl Uppiano wrote:
    >
    > Holy crap! I hope no one ever asks me to evaluate code like that on a
    > job interview. I would never write code like that, but I doubt if I'd
    > squirt that nuance from the top of my head.


    I'll have to make that an interview question. If you answer anything other
    than "I would *never* write code like that", you wouldn't get the job.
     
    Mike Schilling, Dec 24, 2006
    #6
  7. Big D

    Karl Uppiano Guest

    "John W. Kennedy" <> wrote in message
    news:v8mjh.1600$...
    > Karl Uppiano wrote:
    >> "John W. Kennedy" <> wrote in message
    >> news:c0ijh.1967$...
    >>> Big D wrote:
    >>>> I'm confused by the output of the following code:
    >>>>
    >>>> public class PP {
    >>>> public static void main(String[] args) {
    >>>> int i = 1;
    >>>> i = i++;
    >>>> System.out.println(i);
    >>>> }
    >>>> }
    >>>>
    >>>> It outputs 1.
    >>>>
    >>>> I understand the assignment operator happening before the ++, but I
    >>>> don't understand why the ++ doesn't increment. I thought the statement
    >>>> should basically expand to:
    >>>>
    >>>> i = i;
    >>>> i = i+1;
    >>>>
    >>>> but the ++ gets lots somewhere...
    >>> No, it gets expanded into:
    >>>
    >>> t = i;
    >>> i = i + 1;
    >>> i = t;
    >>>
    >>> Don' do dat!
    >>>
    >>> --
    >>> John W. Kennedy

    >>
    >> Holy crap! I hope no one ever asks me to evaluate code like that on a job
    >> interview. I would never write code like that, but I doubt if I'd squirt
    >> that nuance from the top of my head.

    >
    > Java is an improvement over its ancestor, C. In C, the official
    > interpretation of the code in question is "whatever the compiler feels
    > like doing". Because Java places such an emphasis on portability, it sets
    > down rules.


    That is a Good Thing(TM). I'm not disputing that. But I doubt I could quote
    chapter and verse from the JLS off the top of my head. I'm an open-book-test
    kind of guy (it's how the real world works anyway. No employer I know of
    would require you to work without access to any kind of reference
    materials). I'm just sayin'...
     
    Karl Uppiano, Dec 24, 2006
    #7
  8. Big D

    Stefan Ram Guest

    "Mike Schilling" <> writes:
    >I'll have to make that an interview question. If you answer anything other
    >than "I would *never* write code like that", you wouldn't get the job.


    Such code might make perfect sense, for example, as part of a
    Java compiler test suite.
     
    Stefan Ram, Dec 24, 2006
    #8
  9. Karl Uppiano wrote:
    > "John W. Kennedy" <> wrote in message
    > news:c0ijh.1967$...
    >> Big D wrote:
    >>> I'm confused by the output of the following code:
    >>>
    >>> public class PP {
    >>> public static void main(String[] args) {
    >>> int i = 1;
    >>> i = i++;
    >>> System.out.println(i);
    >>> }
    >>> }
    >>>
    >>> It outputs 1.
    >>>
    >>> I understand the assignment operator happening before the ++, but I don't
    >>> understand why the ++ doesn't increment. I thought the statement should
    >>> basically expand to:
    >>>
    >>> i = i;
    >>> i = i+1;
    >>>
    >>> but the ++ gets lots somewhere...

    >> No, it gets expanded into:
    >>
    >> t = i;
    >> i = i + 1;
    >> i = t;
    >>
    >> Don' do dat!
    >>
    >> --
    >> John W. Kennedy

    >
    > Holy crap! I hope no one ever asks me to evaluate code like that on a job
    > interview. I would never write code like that, but I doubt if I'd squirt
    > that nuance from the top of my head.
    >
    >


    Variations on the exact effect of various combinations of prefix/postfix
    ++/-- are frequently asked questions in this newsgroup. I've seen the
    questions often enough to remember how to work out the answers. The fact
    that the JLS has a very simple, consistent, well-defined model makes
    them quite easy to answer.

    The only time I've ever coded something like that in Java was when
    checking my answers for more complicated cases.

    Patricia
     
    Patricia Shanahan, Dec 24, 2006
    #9
  10. Big D

    Guest

    Hi, this is not gonna work like you write it. Look if you do i = i++;
    it will asign i = i because it evaluates i and after that it
    increments.

    You should do:

    i = ++i;

    If you only want to increment "i" then just do:

    public static int main(String[] args) {
    int i = 1;
    i++;
    System.out.println("The value of variable i is: " + i );
    return 0;
    }
    }

    Output = 'The value of variable i is: 2'

    > I'm confused by the output of the following code:
    >
    > public class PP {
    > public static void main(String[] args) {
    > int i = 1;
    > i = i++;
    > System.out.println(i);
    > }
    > }
    >
    > It outputs 1.
    >
    > I understand the assignment operator happening before the ++, but I don't
    > understand why the ++ doesn't increment. I thought the statement should
    > basically expand to:
    >
    > i = i;
    > i = i+1;
    >
    > but the ++ gets lots somewhere...
     
    , Dec 24, 2006
    #10
  11. wrote:
    > Hi, this is not gonna work like you write it. Look if you do i = i++;
    > it will asign i = i because it evaluates i and after that it
    > increments.


    You are right about the result, as far as it goes, but why are you
    giving a confusing and imprecise explanation after two of us have given
    exact descriptions?

    > You should do:
    >
    > i = ++i;


    No, he shouldn't. It's true that this statement works, but it's stupid.
    The correct statement is:

    ++i;

    or

    i++;

    (I personally prefer the first, but the second is more common in the wild.)

    And don't top-post.

    --
    John W. Kennedy
    "The blind rulers of Logres
    Nourished the land on a fallacy of rational virtue."
    -- Charles Williams. "Taliessin through Logres: Prelude"
     
    John W. Kennedy, Dec 24, 2006
    #11
  12. "Big D" <> wrote in message
    news:Xns98A2B1DDE97D3supagoatatverizonDne@216.196.97.142...
    > I'm confused by the output of the following code:
    >
    > public class PP {
    > public static void main(String[] args) {
    > int i = 1;
    > i = i++;
    > System.out.println(i);
    > }
    > }
    >
    > It outputs 1.
    >
    > I understand the assignment operator happening before the ++, but I don't
    > understand why the ++ doesn't increment. I thought the statement should
    > basically expand to:
    >
    > i = i;
    > i = i+1;
    >
    > but the ++ gets lots somewhere...



    Yes

    ++ is a function, which does two things - returns a value, and changes the
    variable.

    i=i++;
    the function i++ must be calculated first before the final assignment is
    completed.

    i= (i++);

    i++ returns i, so you can reduce it to

    i=i;

    In other words, the ++ is not being lost, it is just being overwritten.

    int i = 1;
    i = i++;

    is the same as writing

    int i = 1;
    int oldval = i;
    i = i + 1;
    i = oldval;

    --
    LTP

    :)
     
    Luc The Perverse, Dec 24, 2006
    #12
  13. "John W. Kennedy" <> wrote in message
    news:B%njh.230$...
    > No, he shouldn't. It's true that this statement works, but it's stupid.
    > The correct statement is:
    >
    > ++i;
    >
    > or
    >
    > i++;


    He didn't write either of them

    He was trying to understand part of a test which deliberately used operators
    in an unusual way to test one's understanding. The practicality of such a
    test, or the implications on actual programming ability are questionable.
    But it should suffice to eliminate those completely inept.

    --
    LTP

    :)
     
    Luc The Perverse, Dec 24, 2006
    #13
  14. "Stefan Ram" <-berlin.de> wrote in message
    news:-berlin.de...
    > "Mike Schilling" <> writes:
    >>I'll have to make that an interview question. If you answer anything
    >>other
    >>than "I would *never* write code like that", you wouldn't get the job.

    >
    > Such code might make perfect sense, for example, as part of a
    > Java compiler test suite.


    As it happens, I don't hire Java compiler writers. For the usual purposes
    of code writing (to implement algorithms), there's no excuse for writing
    code whose meaning is that obscure, any more than there's an excuse for

    int sum(int []arr)
    {
    int sum = 0;
    int i = 0;
    try {
    while(true) {
    sum += arr[i++];
    } catch (ArrayIndexOutOfBoundsException ex) {
    return sum;
    }
    }
     
    Mike Schilling, Dec 24, 2006
    #14
  15. Karl Uppiano wrote:
    > That is a Good Thing(TM). I'm not disputing that. But I doubt I could quote
    > chapter and verse from the JLS off the top of my head. I'm an open-book-test
    > kind of guy (it's how the real world works anyway. No employer I know of
    > would require you to work without access to any kind of reference
    > materials). I'm just sayin'...


    Agreed. In fact, if you're even thinking of reaching for an official
    specification or asking a language lawyer to be sure of what some code
    will do, it's a sign that the code in question is excessively obscure
    and should be rewritten to be more self-documenting.

    If I were giving a job interview, and I did include code like that on
    it, the best answer I would be looking for from a potential hire would
    be "I don't know for sure, but I wouldn't write code like that anyway,
    and I'd hope to hell anyone who did would document it for me." :)
     
    John Ersatznom, Dec 24, 2006
    #15
  16. Big D

    Big D Guest

    Thanks for the explanation, all. Obviously the temporary variable was the
    key to this caper.
     
    Big D, Dec 24, 2006
    #16
  17. John Ersatznom wrote:
    > Karl Uppiano wrote:
    >> That is a Good Thing(TM). I'm not disputing that. But I doubt I could
    >> quote chapter and verse from the JLS off the top of my head. I'm an
    >> open-book-test kind of guy (it's how the real world works anyway. No
    >> employer I know of would require you to work without access to any
    >> kind of reference materials). I'm just sayin'...

    >
    > Agreed. In fact, if you're even thinking of reaching for an official
    > specification or asking a language lawyer to be sure of what some code
    > will do, it's a sign that the code in question is excessively obscure
    > and should be rewritten to be more self-documenting.
    >
    > If I were giving a job interview, and I did include code like that on
    > it, the best answer I would be looking for from a potential hire would
    > be "I don't know for sure, but I wouldn't write code like that anyway,
    > and I'd hope to hell anyone who did would document it for me." :)


    Oh well, that's one job I would never get. Personally, I think knowing
    exactly what statements with multiple side effects do is useful, because
    it makes it easier to replace them with functionally equivalent but more
    readable code. It's not as though the Java model of ++ and -- is in any
    way messy, unmemorable, or difficult to understand.

    Patricia
     
    Patricia Shanahan, Dec 24, 2006
    #17
  18. Patricia Shanahan wrote:
    >> If I were giving a job interview, and I did include code like that on
    >> it, the best answer I would be looking for from a potential hire would
    >> be "I don't know for sure, but I wouldn't write code like that anyway,
    >> and I'd hope to hell anyone who did would document it for me." :)

    >
    > Oh well, that's one job I would never get. Personally, I think knowing
    > exactly what statements with multiple side effects do is useful, because
    > it makes it easier to replace them with functionally equivalent but more
    > readable code. It's not as though the Java model of ++ and -- is in any
    > way messy, unmemorable, or difficult to understand.


    It's the "I wouldn't write code like that anyway" part I'd consider most
    important. If you could clean up code like that written by someone else,
    so much the better, especially if you could do it even when the "someone
    else" hadn't documented it. :)
     
    John Ersatznom, Dec 24, 2006
    #18
  19. Big D

    jupiter Guest

    "John W. Kennedy" <> wrote in message
    news:c0ijh.1967$...
    > Big D wrote:
    >> I'm confused by the output of the following code:
    >>
    >> public class PP {
    >> public static void main(String[] args) {
    >> int i = 1;
    >> i = i++;
    >> System.out.println(i);
    >> }
    >> }
    >>
    >> It outputs 1.
    >>
    >> I understand the assignment operator happening before the ++,
    >> but I don't understand why the ++ doesn't increment. I thought
    >> the statement should basically expand to:
    >>
    >> i = i;
    >> i = i+1;
    >>
    >> but the ++ gets lots somewhere...

    >
    > No, it gets expanded into:
    >
    > t = i;


    John, is t a copy of the bits in i made by the postfix ++ method?

    > i = i + 1;
    > i = t;


    Is it fair then to say that t gets restored because of the way the
    postfix operator applies only after the expression is evaluated?

    What is the difference between the original problem and this one?

    int i=1;
    System.out.println("i=" + i++); //output=1
    System.out.println("i=" + i);//output=2

    I find this one easier to understand because eventually the new
    bits do output. They don't get overwritten. In the original problem
    there is overwriting going on that cause the 2 value to be lost
    entirely. Is this due to the self-referencing aspect of the
    original problem i=i++;?

    One last question: Where is that temp copy of the bits visible? I
    can't see it in a debugger, so maybe I'm not using it (Eclipse) to
    great advantage. I see the original i bits on the stack and that's
    about it. This should not involve heap storage, so that leaves me
    wondering where/how I'd ever see that temp set of bits.

    thanks ...
     
    jupiter, Dec 24, 2006
    #19
  20. jupiter wrote:
    > "John W. Kennedy" <> wrote in message
    > news:c0ijh.1967$...
    >> Big D wrote:
    >>> I'm confused by the output of the following code:
    >>>
    >>> public class PP {
    >>> public static void main(String[] args) {
    >>> int i = 1;
    >>> i = i++;
    >>> System.out.println(i);
    >>> }
    >>> }
    >>>
    >>> It outputs 1.
    >>>
    >>> I understand the assignment operator happening before the ++,
    >>> but I don't understand why the ++ doesn't increment. I thought
    >>> the statement should basically expand to:
    >>>
    >>> i = i;
    >>> i = i+1;
    >>>
    >>> but the ++ gets lots somewhere...

    >> No, it gets expanded into:
    >>
    >> t = i;

    >
    > John, is t a copy of the bits in i made by the postfix ++ method?


    For explanatory purposes, I've eliminated the ++ altogether.

    >> i = i + 1;
    >> i = t;


    > Is it fair then to say that t gets restored because of the way the
    > postfix operator applies only after the expression is evaluated?


    My t does not exist in the postfix version at all, so in one sense the
    question is meaningless, but yes, I guess you could say so.

    > What is the difference between the original problem and this one?


    > int i=1;
    > System.out.println("i=" + i++); //output=1
    > System.out.println("i=" + i);//output=2


    It doesn't have the expression "i = i++", which is bad coding, though
    technically legal.

    > I find this one easier to understand because eventually the new
    > bits do output. They don't get overwritten. In the original problem
    > there is overwriting going on that cause the 2 value to be lost
    > entirely. Is this due to the self-referencing aspect of the
    > original problem i=i++;?


    Yes, that's the key.

    > One last question: Where is that temp copy of the bits visible? I
    > can't see it in a debugger, so maybe I'm not using it (Eclipse) to
    > great advantage. I see the original i bits on the stack and that's
    > about it. This should not involve heap storage, so that leaves me
    > wondering where/how I'd ever see that temp set of bits.


    It isn't visible at the Java level, though it will be seen in the
    bytecode (does Eclipse have an option to trace at the bytecode level?
    I've never had occasion to try). But "i=i++" is bad, evil, wicked,
    perverse, satanic, blood-sucking, kitten-murdering, baby-raping,
    Bush-voting malpractice, so Just Don't Do It.

    --
    John W. Kennedy
    "The blind rulers of Logres
    Nourished the land on a fallacy of rational virtue."
    -- Charles Williams. "Taliessin through Logres: Prelude"
     
    John W. Kennedy, Dec 24, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nagy
    Replies:
    36
    Views:
    1,011
    Terry Reedy
    Jul 20, 2006
  2. Replies:
    104
    Views:
    11,024
    Jordan Abel
    Oct 28, 2005
  3. Replies:
    99
    Views:
    2,510
    eliza81
    Jun 11, 2010
  4. Alf P. Steinbach /Usenet
    Replies:
    0
    Views:
    898
    Alf P. Steinbach /Usenet
    May 22, 2011
  5. Peng Yu

    post increment or pre increment?

    Peng Yu, Nov 21, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    529
    Peter J. Holzer
    Nov 23, 2008
Loading...

Share This Page