1

An example of thee shorthand Java Arithmetic operator is a += 4; for a=a+4;

In The Complete Reference, Java 2, Herbert Schildt mentions "they are implemented more efficiently by the Java run-time system than are their equivalent"

What makes its implementation more efficient than a=a+4;

erhun
  • 3,549
  • 2
  • 35
  • 44
  • 2
    Try to generate the bytecode for each and compare them. If you don't know how to compare them, then post them in your question and somebody here will help you. – Luiggi Mendoza Nov 13 '13 at 15:03
  • I think it's actually called compound assignment operators, right? – Zong Nov 13 '13 at 15:04
  • @ZongZhengLi Yea its the same thing, mentioned in different names in different literature –  Nov 13 '13 at 15:05
  • @LuiggiMendoza How do I generate/view bytecode? –  Nov 13 '13 at 15:06
  • @LuiggiMendoza: The bytecodes are a start, but in the presence of a JIT compiler wouldn't necessarily be definitive as far as final performance is concerned. – NPE Nov 13 '13 at 15:07
  • Use [javap](http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javap.html). More formally, use `javap -c` – Luiggi Mendoza Nov 13 '13 at 15:07
  • @NPE you're right since in the end the JVM will perform them. So, it will be a matter of JVM micro benchmarking (as you posted in your answer). – Luiggi Mendoza Nov 13 '13 at 15:08
  • 1
    @anakin you can check here too [http://stackoverflow.com/questions/9292383/is-there-any-performance-difference-between-using-int-a-a1-and-a-in-java-if] – erhun Nov 13 '13 at 15:08

4 Answers4

6

Only microbenchmarks can confirm or reject the author's claim in any given execution environment.

On a modern JVM, it is more likely than not that the two versions will exhibit identical performance.

P.S. If the "2" in the book title is as in "Java 2", I'd strongly recommend getting a more up-to-date book!

NPE
  • 486,780
  • 108
  • 951
  • 1,012
4

For a += 4 javac can produce IIC instruction which does Increment local variable by constant. It is theoretically more efficient then IADD.

IADD does Add int with popping up two values from stack then pushing back the result. IIC does nothing on stack but increments local variable.

So if you may work on a very limited and primitive JVM like you may found on a Java Card this may matter but in %99.9 of the scenarios it does not. Java, JVM and most other virtual machines came a long way.

Btw which edition of the book, do you use? Amazon mentions it will have a 9th version in 2014. It would surprise me if that line is still in the book.

auselen
  • 27,577
  • 7
  • 73
  • 114
  • are you serious? a+=10 is faster and java can't optimize to that at compile time? – Cruncher Nov 13 '13 at 15:17
  • @Cruncher does my late edit satisfies as an answer? – auselen Nov 13 '13 at 15:20
  • My question is more along the lines of compile time optimization, not runtime. If `a+=4` is faster, why not compile `a=a+4` to it automatically(that is, generate identical bytecode for 2 programs that differ only by this line)? Seems like a rather trivial optimization for a compiler. – Cruncher Nov 13 '13 at 15:22
  • @Cruncher Do an experiment yourself, for me `javac 1.6` doesn't optimize that. It is somehow down side of the JIT. Compiler creates very simple code and Java ecosystem depends on JIT to improve those on runtime. A few years ago there where explicit articles about why developers should write dump code (http://www.oracle.com/technetwork/articles/javase/devinsight-1-139780.html) - I think javac follows the same principle and creates dumb code. – auselen Nov 13 '13 at 15:26
1

As with the unary increment operator (++) both versions are identical concerning performance as the same bytecode is generated out of them (at least using the Eclipse JDT).

isnot2bad
  • 24,105
  • 2
  • 29
  • 50
0

a+=4 : it gives implicit type conversion. for example consider a+=4.4 it automatically coverts 4.4 to 4. but a = a+4.4 give you casting error.

subash
  • 3,116
  • 3
  • 18
  • 22
  • To me, this argument sounds like you're claiming that `a=a+4` is faster because it assumes correct types. – Cruncher Nov 13 '13 at 15:18