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


Groups > comp.lang.java.help > #697

Re: Why would one use += 1 at the place of ++?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail
From Lew <noone@lewscanon.com>
Newsgroups comp.lang.java.help
Subject Re: Why would one use += 1 at the place of ++?
Date Thu, 19 May 2011 08:43:18 -0400
Organization albasani.net
Lines 42
Message-ID <ir338b$c4p$1@news.albasani.net> (permalink)
References <8762p7upgn.fsf@merciadriluca-station.MERCIADRILUCA> <4t6Bp.534$pi2.511@newsfe11.iad>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
X-Trace news.albasani.net OfgyOlmYVeacjHLDjhzMY5KBOt2qvTKzXpD42ODA6FdPesQsBRXIpkDgs1ZZuluy7a7Qw5wHf5bJub9CzqtFfyNzORqF4sp9A6TUeahrQEjM931LE7vl+lIyCcUBipLp
NNTP-Posting-Date Thu, 19 May 2011 12:42:51 +0000 (UTC)
Injection-Info news.albasani.net; logging-data="GoMjIEtNJ9C6nqi09WFvc1ML0inKKMazIgG74I255lmu0P2XK8/q5A59O97UEexmxzg/J59w/nLXviPGG3g7+xt81JmTU09UYNOlO+70AOE9Dj2FvSITPHFUJiHjXOa5"; mail-complaints-to="abuse@albasani.net"
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110424 Thunderbird/3.1.10
In-Reply-To <4t6Bp.534$pi2.511@newsfe11.iad>
Cancel-Lock sha1:t0XiF08eWRO4xJ7E4Lw43wE0WbM=
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.help:697

Show key headers only | View raw


David Lamb wrote:
> Merciadri Luca wrote:
>> I've heard that it is generally better to use += 1 at the place of ++. For me,
>> they do exactly the same thing, maybe except that the former is
>> actually splitted into different atomic operations, when the latter
>> might be done in an atomic operation?

> I think ++ exists only because the hardware on which C was developed had an
> addressing mode that did it atomically in the very special case of referring to
> something = array[index++];
> I have no idea what goes on with the Java byte code specifically (ie whether
> ++ is defined to be atomic) but any decent optimizer ought to treat x++ and x
> +=1 each in whatever way is optimal.

Nope.

++ is not atomic.  This has nothing to do with being "optimal" or not.  Also, 
its semantics are not the same as += 1.  By definition, 'x += 1;' is identical 
in operation to 'x = (type) (x + 1);' except for the number of times the 
variable is evaluated.

"A compound assignment expression of the form E1 op= E2 is equivalent to E1 = 
(T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only 
once."
- JLS, ยง15.26.2

The op= construction works on all types that support the 'op'.  ++ only works 
on numeric types.

The post- and pre-increment operators and compound assignment operators have 
different precedence.

There's no cast, widening or narrowing conversions or value set conversion in 
a pre- or post-increment operator expression.

And of course, the post-increment operators yield a different expression value 
from the pre-increment or compound assignment operators.

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

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


Thread

Why would one use += 1 at the place of ++? Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-05-19 09:19 +0200
  Re: Why would one use += 1 at the place of ++? Bent C Dalager <bcd@pvv.ntnu.no> - 2011-05-19 10:22 +0000
  Re: Why would one use += 1 at the place of ++? David Lamb <dalamb@cs.queensu.ca> - 2011-05-19 06:40 -0400
    Re: Why would one use += 1 at the place of ++? Jeff Higgins <jeff@invalid.invalid> - 2011-05-19 07:58 -0400
    [OT] Re: Why would one use += 1 at the place of ++? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-19 08:03 -0400
      Re: [OT] Re: Why would one use += 1 at the place of ++? Patricia Shanahan <pats@acm.org> - 2011-05-19 07:35 -0700
        Re: [OT] Re: Why would one use += 1 at the place of ++? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-19 22:57 -0400
          Re: [OT] Re: Why would one use += 1 at the place of ++? rossum <rossum48@coldmail.com> - 2011-05-20 11:00 +0100
          Re: [OT] Re: Why would one use += 1 at the place of ++? Patricia Shanahan <pats@acm.org> - 2011-05-20 12:55 -0700
      Re: [OT] Re: Why would one use += 1 at the place of ++? David Lamb <dalamb@cs.queensu.ca> - 2011-05-19 17:58 -0400
        Re: [OT] Re: Why would one use += 1 at the place of ++? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-19 23:13 -0400
    Re: Why would one use += 1 at the place of ++? Lew <noone@lewscanon.com> - 2011-05-19 08:43 -0400
  Re: Why would one use += 1 at the place of ++? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-19 07:51 -0400
  Re: Why would one use += 1 at the place of ++? Jeff Higgins <jeff@invalid.invalid> - 2011-05-19 08:03 -0400
    Re: Why would one use += 1 at the place of ++? Lew <noone@lewscanon.com> - 2011-05-19 08:08 -0400
  Re: Why would one use += 1 at the place of ++? Roedy Green <see_website@mindprod.com.invalid> - 2011-05-19 10:23 -0700
    Re: Why would one use += 1 at the place of ++? Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-05-19 21:15 +0200
      Re: Why would one use += 1 at the place of ++? Lew <noone@lewscanon.com> - 2011-05-19 15:29 -0400

csiph-web