• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

post operator vs pre operator

 
Greenhorn
Posts: 15
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Sample program:
 n = 0;
 a = ++n;
 b = n++;

If the post-increment or post-decrement operator has higher precedence than the pre-increment or pre-decrement operator, then why b didn't get a value of 2? I mean, it would make more logical sense if a = 0 and b = 2 with the precedence of the two.

I guess my question is, if n++ has higher precedence, why didn't it apply the ++ effect sooner than the ++ on the ++n? Is this just a special case in Java?

Thanks
 
Bartender
Posts: 242
27
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, in "a = ++n", the ++n operator means the ++ takes place first. In other words, it's essentially doing the following: "n++; a = n"
In "b = n++", the ++ happens last. So you can think of it as: "b = n; n++"

I think you got the two operators mixed up.

In practice, these command should probably be split into two when you write code. Saving one line of code isn't worth the confusion caused by things like this!

If you run this program:


vs, this program:



They are functionally equivalent, but the second one is much more clear than the other.
 
Sheriff
Posts: 26773
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zachary Griggs wrote:So, in "a = ++n", the ++n operator means the ++ takes place first. In other words, it's essentially doing the following: "n++; a = n"
In "b = n++", the ++ happens last. So you can think of it as: "b = n; n++"



That's a pretty good way of describing it, I haven't seen that before.
 
Marshal
Posts: 74004
332
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But ZG said that CC had got it all mixed up. He said that isn't how it happens.
This problem comes up frequently, and there is a question in the Boyarsky and Selikoff book using those two operators, which causes great confusion to its readers. Let's see if I can't find some more information. Note strange grammatical idiom with “can't”, which proves I have lived in London Yes, here are three threads discussing it and there are lots more in the cert exam forum: links: 1 2 3. Please also check the link in the first post in thread 3. We also have an FAQ; as I said this confuses many people and we receive many questions about that.
If you go through the JLS (=Java® Language Specification) you will find that there are two values for expressions including ++ or --. (You can only use those operators once per operand.) Those expressions produce two values, one that of the operand and the other that of the whole expression. In the case of ++x, we can only see the value of the whole expression, but it is the same as the (hidden) value of x.In the case of x++ however, the hidden value of x is the new value, but the (visible) value of the whole expression x++ is the same as the old value of x. You cannot see the new value of x until the next time it is used.Here the whole expression j++ has the old value of j and the incremented value is hidden until the next time you use j.

To avoid confusion, I suggest four rules:-
  • 1: Compound increment and decrement are permissible in cert exams and cert exams revision code.
  • 2: Compound increment and decrement are permissible in “I just want to see what happens if...” code.
  • 3: Compound increment and decrement are permissible as array indices in the implementation of a data structure.
  • That code can be changed to the following to avoid potential confusion:-
  • 4: Compound increment and decrement are not permissible anywhere else.
  • Remember that ++x; and x++; can always be written as single statements and like that, any differences will be completely hidden and cannot be seen.
     
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote: Here the whole expression j++ has the old value of j and the incremented value is hidden until the next time you use j.



    Based on the above,



    I understand line 2 and 3 . W.r.t lines 5 , 6 , Is it because in j = j++, the assignment operator j = j executes first with the assignment operator returning 124 and the 124 is incremented to 125 but not assigned to any variable ?
    Can you please clarify if my understanding is correct ?
     
    vvikram sri
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Nervermind ! I found the explanation for this in the answer of review question 8 of OCP Java SE 11 Developer Complete Study Guide  .
    The increment operation is discarded.
     
    Cosmid Constantine
    Greenhorn
    Posts: 15
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Zachary Griggs wrote:So, in "a = ++n", the ++n operator means the ++ takes place first. In other words, it's essentially doing the following: "n++; a = n"
    In "b = n++", the ++ happens last. So you can think of it as: "b = n; n++"



    That is a good explanation. Thank you. My confusion wasn't about the final value of a or b from n++ or ++n. I understand how both the pre and post increment and decrement works. What confused me was the precedence of the two. Like I said in my question earlier, if post increment has a higher precedence than pre increment, why does it take effect later than pre. I mean, shouldn't Java use the pre increment to hide the increased value?

    So my understanding is that even though the n++ has higher precedence and is evaluated first than other operators, but it just has one special talent, it hides its calculated value.

    I am bad at asking questions. It's hard for me to express my question. Sorry about that. And thanks for everyone's help!
     
    Campbell Ritchie
    Marshal
    Posts: 74004
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Cosmid Constantine wrote:. . . . if post increment has a higher precedence than pre increment, why does it take effect later than pre. . . . n++ has higher precedence . . . but . . . it hides its calculated value. . . .

    I think that is an accurate way to describe x++, yes.
    If you use =, that is evaluated later and overwrites the hidden increment.
     
    Campbell Ritchie
    Marshal
    Posts: 74004
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    O, somebody has given me a cow. Thank you, whoever it was.
     
    Cosmid Constantine
    Greenhorn
    Posts: 15
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    What's a cow? I mean, what does a cow mean on here.
     
    Campbell Ritchie
    Marshal
    Posts: 74004
    332
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Cow link.
     
    Cosmid Constantine
    Greenhorn
    Posts: 15
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That's awesome-Congratz!
     
    Campbell Ritchie
    Marshal
    Posts: 74004
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you
     
    Ranch Hand
    Posts: 91
    Eclipse IDE Debian Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Cosmid Constantine wrote:Hi,

    Sample program:
     n = 0;
     a = ++n;
     b = n++;

    If the post-increment or post-decrement operator has higher precedence than the pre-increment or pre-decrement operator, then why b didn't get a value of 2? I mean, it would make more logical sense if a = 0 and b = 2 with the precedence of the two.

    I guess my question is, if n++ has higher precedence, why didn't it apply the ++ effect sooner than the ++ on the ++n? Is this just a special case in Java?

    Thanks



    Here n = 0; that's clear. n is given the value 0. Okay then a = ++n; which means prefix increment operator is used. Here a is assigned the value after n has been incremented by 1. So a is equal to 1 now. Then b = n++; postfix increment operator is used. b is assigned the current value of n first which is 1 then n is incremented. Now n is equal to 2 and b is equal to 1

    And if you do like this:




    You can see the result now.
    But if you had



    All by itself it doesn't matter. n will be incremented anyways.
    But for n++; it is expensive for huge numbers.

    n is evaluated first then n is incremented.
    But if you had

    ++n;

    It is little bit less expensive for huge numbers.

    Why you should avoid postfix operator?

    I hope it helps.

     
    Campbell Ritchie
    Marshal
    Posts: 74004
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Mark Ii wrote:. . . postfix increment operator is used. b is assigned the current value of n first which is 1 then n is incremented. . . .

    Afraid that is incorrect. The increment to n occurs first. But the value of n++ is not incremented, i.e. n++ has the old value of n.
     
    Mark Ii
    Ranch Hand
    Posts: 91
    Eclipse IDE Debian Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Mark Ii wrote:. . . postfix increment operator is used. b is assigned the current value of n first which is 1 then n is incremented. . . .

    Afraid that is incorrect. The increment to n occurs first. But the value of n++ is not incremented, i.e. n++ has the old value of n.



    Thank you for the correction.
     
    Campbell Ritchie
    Marshal
    Posts: 74004
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    For having a question mentioned in the December 2020 CodeRanch Journal, congratulations: this question earns you a cow
     
    Campbell Ritchie
    Marshal
    Posts: 74004
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    For having a question mentioned in the December 2020 CodeRanch Journal, congratulations: this question earns you a cow
     
    My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic