Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #9056

Re: A freshman's question

From Joshua Cranmer <Pidgeot18@verizon.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: A freshman's question
Date 2011-10-20 17:38 -0500
Organization A noiseless patient Spider
Message-ID <j7q7sf$qhp$1@dont-email.me> (permalink)
References <3242b80f-ee51-45f0-9e12-231482f61a97@r2g2000prh.googlegroups.com> <37KdnWZAYfIR1QPTnZ2dnUVZ_vWdnZ2d@earthlink.com> <j7pbaa$92n$1@dont-email.me> <r7l0a7tnf16mukam36n816b6t4jed0p34g@4ax.com>

Show all headers | View raw


On 10/20/2011 12:08 PM, Gene Wirchenko wrote:
> On Thu, 20 Oct 2011 07:30:31 -0700, Travers Naran<tnaran@gmail.com>
> 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 <http://cm.bell-labs.com/cm/cs/who/dmr/chist.html>. 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

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

A freshman's question whl <wanghailunmail@gmail.com> - 2011-10-18 20:08 -0700
  Re: A freshman's question Patricia Shanahan <pats@acm.org> - 2011-10-19 04:48 +0100
    Re: A freshman's question whl <wanghailunmail@gmail.com> - 2011-10-19 00:54 -0700
      Re: A freshman's question Patricia Shanahan <pats@acm.org> - 2011-10-19 09:20 +0100
        Re: A freshman's question whl <wanghailunmail@gmail.com> - 2011-10-19 03:38 -0700
          Re: A freshman's question Lars Enderin <lars.enderin@telia.com> - 2011-10-19 17:48 +0200
            Re: A freshman's question Tim Slattery <Slattery_T@bls.gov> - 2011-10-19 12:24 -0400
              Re: A freshman's question whl <wanghailunmail@gmail.com> - 2011-10-20 03:55 -0700
            Re: A freshman's question whl <wanghailunmail@gmail.com> - 2011-10-20 03:50 -0700
          Re: A freshman's question Lew <lewbloch@gmail.com> - 2011-10-19 09:58 -0700
            Re: A freshman's question Patricia Shanahan <pats@acm.org> - 2011-10-19 18:47 +0100
            Re: A freshman's question whl <wanghailunmail@gmail.com> - 2011-10-20 03:48 -0700
        Re: A freshman's question whl <wanghailunmail@gmail.com> - 2011-10-19 03:49 -0700
          Re: A freshman's question Patricia Shanahan <pats@acm.org> - 2011-10-19 18:58 +0100
            Re: A freshman's question whl <wanghailunmail@gmail.com> - 2011-10-20 02:13 -0700
      Re: A freshman's question Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-10-19 06:05 -0300
        Re: A freshman's question whl <wanghailunmail@gmail.com> - 2011-10-19 03:38 -0700
    Re: A freshman's question Travers Naran <tnaran@gmail.com> - 2011-10-20 07:30 -0700
      Re: A freshman's question Gene Wirchenko <genew@ocis.net> - 2011-10-20 10:08 -0700
        Re: A freshman's question Patricia Shanahan <pats@acm.org> - 2011-10-20 18:22 +0100
        Re: A freshman's question Lew <lewbloch@gmail.com> - 2011-10-20 13:59 -0700
        Re: A freshman's question Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-10-20 17:38 -0500
  Re: A freshman's question Roedy Green <see_website@mindprod.com.invalid> - 2011-10-19 23:56 -0700
    Re: A freshman's question whl <wanghailunmail@gmail.com> - 2011-10-20 03:55 -0700

csiph-web