This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is there a URL that explains[ y = x++ + ++x; ] ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is there a URL that explains[ y = x++ + ++x; ] ?" Watch "Is there a URL that explains[ y = x++ + ++x; ] ?" New topic
Author

Is there a URL that explains[ y = x++ + ++x; ] ?

Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
Is there a URL that explains[ y = x++ + ++x; ] ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60766
    
  65

www.coderanch.com


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60766
    
  65

In other words, you could ask here. After all, there isn't going to be a URL for each permutation of a Java expression.
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
Hi,

This is a question from the SCJP 5.0 WhizLabs Diagnostic exam. Unfortunately, I didn't write down the answers in the exam. Will someone tell me how to calculate the answer for this and similar problems?

I realize that in the case of x++ and y--, the values of x and y are returned BEFORE the increment or decrement operation. In the case of ++x and --y, the values are returned AFTER the increment or decrement operation. This realization doesn't help me understand how to calculate the final result.

Thanks,
Harry


Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Hey Harry,

As we know that + and -has its associatevity from left to right . I have put the numbers explaining how this is calculated.



I hope this would clear your confusion.


Regards
Salil Verma
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

See if this FAQ helps...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Try wolframalpha.com. It claims to know everything that is computable.
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206


C:\Documents and Settings\Harry Henriques>java Expo
int x = 45
int y = 12
int a = 45
int b = 45
int c = 45
int d = 12

a = a + 1 = 46
b = a - 1 = 45
int bb = a + b = 91

int cc = ((c++) + (c--)) = a + b = 91
x = x++ + x-- = 91

x = x++ + y-- - ++y = 91
int dd = ((bb++) + (d--) - (++d)) = 91

System.out.println( --x); prints 90
System.out.println( --dd); prints 90
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
Hi, I could use some help on this one. I thought that I had this issue figured out. I thought that it was strictly a matter of Operator precedence and left to right Operator associativity when calculating the rvalue. But, that is not the case. There is a more complex mechanism that is operational in these type of problems. I can't figure out what that mechanism is. Can you give me a hand? Look at the attached code block. A comparison with the code block in the preceding post will make the problem clear.

This is the basic problem.



I have expanded on the problem in the code block below.



C:\Documents and Settings\Harry Henriques>java Expo
int x = 45
int y = 12
int a = 45
int b = 45
int c = 45
int d = 12

a = a + 1 = 46
b = a + 1 = 47
int bb = a + b = 93

int cc = ((c++) + (c++)) = 91
x = x++ + x++ = 91

x = x++ + y++ - ++y = 89
= 92 + 13 - 14 = 91 ???

e = e + 1 = 13
f = 1 + e = 14
ee = bb + 1 = 94
ff = ee + e - f = 93
ff =((bb+=1) + (e+=1) - (e+=1)) = 93

int dd = ((bb++) + (d++) - (++d)) = 91

System.out.println( --x); prints 88 // these two numbers should agree. the correct answer is 88, not 90.
System.out.println( --dd); prints 90
Uma Bandaru
Ranch Hand

Joined: Jul 30, 2009
Posts: 31
Harry,

We just have to be clear that in x++, the value of x used is the PRE-INCREMENTED value (value before increment) and in ++x, it the POST-INCREMENTED value (value after the increment).
Then all you need to do is keep replacing x's value in the expression.
Let us say x = 3.

(x++) + (++x) becomes:
3 + (++4), which is 3 + 5, which is 8.

Hope that helped.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

these two numbers should agree. the correct answer is 88, not 90.


I don't understand the "should agree" part -- these look like two different expressions.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40


To walk through the "basic problem"...

int x = 45;
int y = 12;

--> x is now 45 and y is now 12

x = x++ + x++;

--> add precedence

x = (x++) + (x++);

--> substitute first part...

x = (45) + (x++);

--> x is now 46; substitute second part...

x = (45) + (46);

--> x becomes 47 after increment, and then becomes 91 after sum and assigment. Next...

x = x++ + y++ - ++y;

--> add precedence

x = (x++) + (y++) - (++y);

--> substitute first part

x = (91) + (y++) - (++y);

--> x is now 92. substitute second part.

x = (91) + (12) - (++y);

--> x is now 92. y is now 13. substitute third part.

x = (91) + (12) - (14);

--> x is now 92. y is now 14 (and since preincrement, 14 is used).

x = 89;

--> x is now 89 and y is now 14. next...

System.out.println(--x);

--> x is now 88. and since predecrement is used 88 is printed.

Henry
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
Henry, I don't see anything wrong with the logic of your previous post, but your post doesn't answer my question. I have listed two examples below that illustrate the problem. In the first example, "x" is post-incremented before the addition operation is effective. In the second example "x" and "y" are added together first, then "x" and "y" each are post-incremented. Do you see what I mean?

Harry




C:\Documents and Settings\Harry Henriques>java Expo1
x = x++ + x++ = 91 // Example 1
x = x++ + y++ - ++y = 89 // Example 2

a = 0
b = 0
c = 0
x = 45

a = (b=x++) + (c=x++) = 91
a = 91
b = 45
c = 46
x = 47

d = (h=((e=a++) + (f=y++))) - (g=++y) = 89
h = 103
e = 91
f = 12
a = 92
y = 14
g = 14





Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

Do you see what I mean?


No... Can you simplify your question? Your example has about a dozen variables being changed in all sort of ways.

Can I use this as the simplified example?



And BTW, what is the question?

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

Actually, in reading your example a bit more, it looks like all the other letters are just the interim stuff, and it matches everything in my walkthrough. What is it about the walk through that didn't answer your question? If you agree with it, shouldn't it apply to your program?

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

Your output...

x = x++ + x++ = 91 // Example 1
x = x++ + y++ - ++y = 89 // Example 2


From the walkthrough...

int x = 45;
int y = 12;

--> x is now 45 and y is now 12

x = x++ + x++;

--> add precedence

x = (x++) + (x++);

--> substitute first part...

x = (45) + (x++);

--> x is now 46; substitute second part...

x = (45) + (46);

--> x becomes 47 after increment, and then becomes 91 after sum and assigment. Next...
*** This is where the first x is printed. When it has a value of 91 ***

x = x++ + y++ - ++y;

--> add precedence

x = (x++) + (y++) - (++y);

--> substitute first part

x = (91) + (y++) - (++y);

--> x is now 92. substitute second part.

x = (91) + (12) - (++y);

--> x is now 92. y is now 13. substitute third part.

x = (91) + (12) - (14);

--> x is now 92. y is now 14 (and since preincrement, 14 is used).

x = 89;

--> x is now 89 and y is now 14. next...
*** This is where the second x is printed. When it has a value of 89 ***



Henry
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
Henry,

Just take for granted that I coded the examples correctly, and look at the output in RED. I'm pretty sure I didn't make any stupid mistakes.

If you will take a look at "the dozen variables", maybe you'll notice that there is something strange happening in the two examples that I listed. In the first example, "x" is post incremented before it is added to the final x++ in the expression (x++ + x++). In the second example, (x++ + y++), "x" is added to "y" before "x" is post incremented and before "y" is post incremented.

How are we supposed to answer questions like this on the SCJP exam? I have found a question like this on two different mock exams. This is like a throw away question. I don't know how to predict the outcome. What would happen if you had a problem like x = x++ + x++ + y++ - ++y or x = x++ + ++x + x++ + y-- I know this question may seems ridiculous, but if there is a "rule" that can be applied in these situations, I would like to know what it is?

Harry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40


Your output...

a = (b=x++) + (c=x++) = 91
a = 91
b = 45
c = 46
x = 47


From the walkthrough...

int x = 45;
int y = 12;

--> x is now 45 and y is now 12

x = x++ + x++;

--> add precedence

x = (x++) + (x++);

--> substitute first part...

x = (45) + (x++);
*** The value of b is the first paren, which is 45 ***

--> x is now 46; substitute second part...

x = (45) + (46);
*** The value of c is the second paren, which is 46 ***

--> x becomes 47 after increment, and then becomes 91 after sum and assigment. Next...
*** The value of a should be the value of x at this point, has a value of 91 ***
*** The value of x should also be 91, but it wasn't assigned in the altered example, so has a value of 47 ***


Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

Your output...

d = (h=((e=a++) + (f=y++))) - (g=++y) = 89
h = 103
e = 91
f = 12
a = 92
y = 14
g = 14


From the walkthrough...

x = x++ + y++ - ++y;

--> add precedence

x = (x++) + (y++) - (++y);

--> substitute first part

x = (91) + (y++) - (++y);
*** The value of e is the first paren, which is 91 ***

--> x is now 92. substitute second part.
*** the value of a is now 92, as you are using a instead of x ***

x = (91) + (12) - (++y);
*** The value of f is the second paren, which is 12 ***
*** The value of h is the first two parens, with is 91 + 12 = 103 ***


--> x is now 92. y is now 13. substitute third part.

x = (91) + (12) - (14);
*** the value of g is the third paren, which is 14 ***

--> x is now 92. y is now 14 (and since preincrement, 14 is used).

x = 89;

--> x is now 89 and y is now 14. next...
*** and the value of y ends at 14 ***


Henry
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
Thanks. It is just very hard for me to predict the order of execution. I'll have to think about it some more.

Thanks.
Harry

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

Harry Henriques wrote:
Just take for granted that I coded the examples correctly, and look at the output in RED. I'm pretty sure I didn't make any stupid mistakes.

If you will take a look at "the dozen variables", maybe you'll notice that there is something strange happening in the two examples that I listed.


Nope. No stupid mistakes. All the interim values (dozen variables) match the interim values calculated in the walkthrough. And I don't see anything strange happening. It looks fine.

Harry Henriques wrote:In the first example, "x" is post incremented before it is added to the final x++ in the expression (x++ + x++). In the second example, (x++ + y++), "x" is added to "y" before "x" is post incremented and before "y" is post incremented.


I am not sure what you mean by this. Can you elaborate a bit? Or are you confusion the interim values to be the interim values of x and y?

Henry
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2231
    
    7

I don't get how come a simple question can turn into a series of responses. Let say both y=0 and x=0 to begin with.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

SCJP6 doesn't focus on precedence operators much and certainly not on questions like x++ + ++y. I guess its said in the K&B guide also. Well about the mock may be its an older version that you have. Well any ways, just keep the prefix and postfix operator concepts and how the value is given to the expression by the operators.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Uma Bandaru
Ranch Hand

Joined: Jul 30, 2009
Posts: 31
Harry,

I don't think there is anything strange happening in your program and you did not make any mistakes either.

In the first expression, x = (x++) + (x++), x is not being inremented BEFORE, it is being incremented AFTER it is used.

So, x = (45)(still 45 is being used, even though x is already 46) + ( 46 ++)

which is (45) + (46) (And here, 46 is being used, even though x is incremented to 47), which is 91. You never use the incremented value in x++, you always use the value before it was incremented.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is there a URL that explains[ y = x++ + ++x; ] ?
 
Similar Threads
Operator precedence
Could u explain this
Another Puzzle
Array question - enthuware
Your political compass