wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Increment operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Increment operator" Watch "Increment operator" New topic

Increment operator

Uttara Rishi
Ranch Hand

Joined: Feb 24, 2008
Posts: 49
Hi all,

Please consider the following code.

At line 1 the output is
x= 1 y=0
I dont understand it....cos if x =1 then y should atleast be 1.
Can somebody please shed some light on it?

Uttara Rishi.
Irina Goble
Ranch Hand

Joined: May 09, 2004
Posts: 91
I'll comment your code.

Uttara Rishi
Ranch Hand

Joined: Feb 24, 2008
Posts: 49
Thanks Irina.That helped.
Higor Zardo
Ranch Hand

Joined: Feb 16, 2008
Posts: 59
y = x++ + ++x; // (-1)++ + ++(0) = -1 + 1 = 0
But x is not -1?
Then ++x = 0, not? because x is -1 and (-1)++ will be avaliate latter of expression y = x++ + ++x.
Ralf Wahner

Joined: Jun 03, 2007
Posts: 21
Dear Ranch People

Uttara Rishis contribution concerning the increment operator includes an aspect of operator behavior I discussed with a colleague a few weeks ago: Let "i" be an integer variable; how to predict the outcome of "i++ + ++i" with respect to Java?

According to Les Hattons "Safer C" (ISBN 0077076400) "in the example a*(f()+g()) although the addition is done before the multiplication, the order in which f() and g() are evaluated is undefined"; see subsection 2.8.2 "Evaluation order and precedence" on page 71 near the bottom.

Assume, that this C behavior applies to Java, we can't tell about the result of "i++ + ++i". Set "i=0". If "i++" is evaluated before "++i", the result is 0+1=1. If "++i" is evaluated first, the result is 1+1=2. Bottom line: We must not use expressions like "i++ + ++i" in our code.

This reply is intended to be both a thought-provoking impulse and a question: Does Java behave the same way like C? Could I ask you to provide a reference where this is declared officially, in either case? Can the compiler be invoked in order to detect situations like this and print a warning?

Best regards,


Tim Weide
Ranch Hand

Joined: Feb 19, 2008
Posts: 37
Is this kind of question likely to appear on the test??
Ralf Wahner

Joined: Jun 03, 2007
Posts: 21
Hello Tim, Dear Ranch People

I don't know if we should expect a question like this on the exam, though it's nasty enough, isn't it? But I don't want to blame the exam preparation commitee. ;-) A question like this might be discussed in an "Exam watch" box in Chapter 4, "Operators" in Kathie Sierras and Bert Bates SCJP book. I'll have a look later. Perhaps there's is a member of the committee around who can help us out here.

Best regards,

John Sutt
Ranch Hand

Joined: Mar 07, 2008
Posts: 42
Here :
it says that evaluation order is guaranteed from left to right.
So, in your example, f() will always be computed before g().

SCJP 6 (91%), SCJD (91%)
Ralf Wahner

Joined: Jun 03, 2007
Posts: 21
Hello Jonne

Thank you very much for your quick reply and the link to the language specification. :-) Unless I now know that evaluating the operands is deterministic, I thought both cases to make sense, i.e. (a) that Java "inherits" the unpredictable behavior from C as well as (b) that the Java creators defined an evaluation order, since brief notation may improve
readability under complex circumstances like certain algorithms, e.g. in numerical mathematics.

You might feel that I overestimate that topic. But now I have an answer for a problem I worried about for a long time. Last but not least, I am a Java greenhorn and I would take years if not decades to sift through the shoreless documentation. Again, thanks a lot; I take your attention and helpfulness as a good example. :-)

Best regards,

Bert Bates

Joined: Oct 14, 2002
Posts: 8803
Hi Guys,

Way back before the 1.4 days you would see questions like this on the exam. You *might* (but I don't think so). get one question like that on the 1.4 exam, but 1.5 and 6 won't go any further than the difference between ++x and x++.



Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
I agree. Here's the link: http://aspose.com/file-tools
subject: Increment operator
Similar Threads
static block understanding
++/-- operator
SCJP Mock Question : static