Results 1 to 7 of 7
- 04-17-2011, 08:42 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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
- 04-17-2011, 11:29 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
//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.
- 04-18-2011, 04:06 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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)?.
- 04-18-2011, 05:06 AM #4
- 04-18-2011, 05:41 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 13
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);
Java Code:1 + 2 + 3
- 04-18-2011, 01:38 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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?.
-
Similar Threads
-
Help with postfix expression
By javajavajava in forum New To JavaReplies: 3Last Post: 11-12-2010, 12:11 PM -
Quick Question about relational operators
By SwEeTAcTioN in forum New To JavaReplies: 2Last Post: 10-27-2009, 06:11 AM -
Postfix this!!
By hasysf in forum New To JavaReplies: 4Last Post: 09-07-2009, 07:44 PM -
Postfix into prefix and vice versa
By sfe23 in forum New To JavaReplies: 9Last Post: 02-19-2009, 11:37 PM -
any can help me? About MDAS and PEMDAS rules and Infix-Prefix, Infix-Postfix rules
By darlineth in forum New To JavaReplies: 1Last Post: 07-05-2008, 04:08 PM
Bookmarks