Results 1 to 7 of 7
  1. #1
    pulikarthi is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default Question regarding prefix & postfix operators

    If we are using variable (b) without postfix/prefix operators along with postfix/prefix on the same variable (b),
    how its value being determined?

    //added () for clarity

    int b = 1; System.out.println((++b) + (b++) + (b));//--> output is 7

    b = 1; System.out.println((++b) + (b) + (b++) ); //--> output is 6

    b = 1; System.out.println((b) + (++b) + (b++)); //--> output is 5

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    //added () for clarity

    Just my opinion, but I think even more clarity is to be achieved by putting the // at the start of the lines...

    Otherwise have a read of the description of unary operators in Oracle's Tutorial. Try predicting what will be output (paying very close attention to the actual wording of the tutorial). If your prediction differs from the actual output you will have a substantial question: be sure to post the runnable code you are using, the actual output and a description of your reasoning.

  3. #3
    pulikarthi is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Hi...thanks...following are my understanding.

    1)

    int b = 1; System.out.println((++b) + (b++) + (b));//--> actual output is 7

    for the above code, am expecting the output as 5

    My understanding -
    b++ gets evaluated first (postfix operator has more precedence) - 1
    last b gets evaluated second - 1
    ++b gets evaluated third - 3

    2)

    b = 1; System.out.println((++b) + (b) + (b++) ); //--> actual output is 6

    am expecting output as 5

    My understanding -
    b++ gets evaluated first (postfix operator has more precedence) - 1
    second b gets evaluated second - 1
    ++b gets evaluated third - 3

    Can someone please let me know why they are returning two different values (7 and 6)?.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by pulikarthi View Post
    My understanding -
    b++ gets evaluated first (postfix operator has more precedence) - 1
    last b gets evaluated second - 1
    ++b gets evaluated third - 3
    Incorrect.

    As pre and post increment have equal precedence the statement is still evaluated left to right

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    When you use post fix you do the add and then increment, with pre fix you increment then add. The evaluation should be evaluated left to right. If it's post fix you should pretend it's b and not increment it, for prefix you increment first.

    For the line
    Java Code:
    int b = 1;
    System.out.println(b++ + b + ++b);
    Is evaluates from left to right and produces something like this
    Java Code:
    1 + 2 + 3
    it adds b, then increments it, the second ocurance of b is now two since it got incremented, in the third part it increments b to 3 first. With that in mind try your best to properly explain how your three print lines work.

  6. #6
    pulikarthi is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the reply. If we are giving same precedence to post fix and pre fix, i am able to understand how this works.

    I have one more thing to ask.

    Based on what i read here, post fix operator has more precedence than prefix operator.

    Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

    " The closer to the top of the table an operator appears, the higher its precedence."

    Post fix appears first box and prefix appears in the second box.

    am i understanding it incorrectly?.

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    all that means is, if you do something like this:

    int b = 10 * 4 + 10;

    since you have no brackets, should the output be 10*14=140 or 40+10=50 ?

    its like BODMAS

Similar Threads

  1. Help with postfix expression
    By javajavajava in forum New To Java
    Replies: 3
    Last Post: 11-12-2010, 11:11 AM
  2. Quick Question about relational operators
    By SwEeTAcTioN in forum New To Java
    Replies: 2
    Last Post: 10-27-2009, 05:11 AM
  3. Postfix this!!
    By hasysf in forum New To Java
    Replies: 4
    Last Post: 09-07-2009, 06:44 PM
  4. Postfix into prefix and vice versa
    By sfe23 in forum New To Java
    Replies: 9
    Last Post: 02-19-2009, 10:37 PM
  5. Replies: 1
    Last Post: 07-05-2008, 03:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •