• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why so?

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In many of the mock SCJP exams the following type of question occurs

int x, a = 6, b = 7;
x = a++ + b++;

After execution of the code fragment above what are the values of x,a and b

The answer given is always
a = 7, b= 8 and x = 13
This is because x is evaluated as a+b and then a++ and b++ are evaluated.

I don't understand why!
According to the operator precedence rules in my java manual unary operators (++) take precedence over arithmetic operators (+).
Is there some other special rule which applies for this case - it appears that the + operator is taking precedence over the ++ operator.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ashok,

Your point about operator precendence is correct but..
Here, we are talking about postfix ++ operator. When it is used as one of the operands on the right hand side, the initial value is taken into consideration and the increment is done afterwards.

Like, int a=10, b;
b = a++; // b will be assigned 10 ; a will be 11

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

Accroding to this

Say X=8 ; If you perform Y = X++;
Then Y=8; and X=9;

If you perform Y = ++X;
Then Y=9; and X=9;

If you perform Y = X--;
Then Y=8; and X=7;

If you perform Y = --X;
Then Y=7 and X =7;


Hope this helps

Regards,
Venkat
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

Enjoy this too

System.out.println(5 + + 5);
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is because x is evaluated as a+b and then a++ and b++ are evaluated.


No, the sequence is as follows:
1) evaluate a++, and push the value onto the stack
vars: {x, 6, 7} stack: {6}

2) increment a
vars: {x, 7, 7} stack: {6}

3) evaluate b++ and push the value onto the stack
vars: {x, 7, 7} stack: {6, 7}

4) increment b
vars: {x, 7, 8} stack: {6, 7}

5) add the values from the stack, and store the result in x
vars: {13, 7, 8} stack: {}

Hope this helps...
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi...

How abt

System.out.println(5 + + 5);

and

System.out.println(5 + + + 5);

System.out.println(5 + + + 5 + + + + + + + 10);

System.out.println(+ 6);


Regards
 
Would you turn that thing down? I'm controlling a mind here! Look ... look at the tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic