Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Joshua Cranmer Newsgroups: comp.lang.java.programmer Subject: Re: A freshman's question Date: Thu, 20 Oct 2011 17:38:02 -0500 Organization: A noiseless patient Spider Lines: 53 Message-ID: References: <3242b80f-ee51-45f0-9e12-231482f61a97@r2g2000prh.googlegroups.com> <37KdnWZAYfIR1QPTnZ2dnUVZ_vWdnZ2d@earthlink.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 20 Oct 2011 22:38:07 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="WpcHJSul77m+zlbR9GVqkA"; logging-data="27193"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX189hDZsKNSakGcGdUqAAAtiTLarrOMV05o=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1 In-Reply-To: Cancel-Lock: sha1:bS1eGGZgE6uIyPqlQDMiSfmO3gY= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:9056 On 10/20/2011 12:08 PM, Gene Wirchenko wrote: > On Thu, 20 Oct 2011 07:30:31 -0700, Travers Naran > wrote: > > [snip] > >> This is off topic, but I just recently learned that in C++, the >> result of i=i++ is officially undefined. It's interesting a >> question related to what I learned comes up again in a completely >> different forum. > > That comes from C. I have a question for anyone thinking it valid > Java. What is it supposed to do of use? (If nothing, why even use > it?) I suspect the whole thing got started by someone not > understanding that ++ causes an assignment to occur. I'm guessing that the largest reason for this issue lies in the fact that ++/-- is largely tied to increment/decrement addressing modes (so *p++ would translate to "Load from p, auto-increment the pointer") [1]. In cases where it would compile to an autoincrement address mode, the effect would take place immediately after the access, much as it does in Java. However, on machines without this mode, it probably made more sense to translate it into "*p; p += 1;". This is, I believe, the reason why the C committee made |i=i++| invalid. In contrast, Java sought to leave nothing undefined in its semantics, so it has to pick a particular point in time at which the ++ takes place. If you're not tied to any overt architecture, the time that makes the most sense is to do it is immediately after getting the value (even before its use!). That means that the following function has a well-defined output: int testWhen() { int i = 0; try { int j = 5 / i++; } catch (Exception e) { return i; } return i; } It is 1: the increment happens before the division. Note that the only point of |i=i++| is to point out that how and when the increment occurs is not fully specified in C, while it is in Java. [1] Apparently, this isn't why they were originally developed, according to . Kind of... the PDP-7 had some memory slots which autoincremented on load, which may have been the spark that caused them to be introduced in the language. -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth