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;

8
  • 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. Commented Nov 13, 2013 at 15:03
  • I think it's actually called compound assignment operators, right? Commented Nov 13, 2013 at 15:04
  • @ZongZhengLi Yea its the same thing, mentioned in different names in different literature Commented Nov 13, 2013 at 15:05
  • @LuiggiMendoza How do I generate/view bytecode? Commented Nov 13, 2013 at 15:06
  • 1
    @anakin you can check here too [stackoverflow.com/questions/9292383/… Commented Nov 13, 2013 at 15:08

4 Answers 4

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!

Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

are you serious? a+=10 is faster and java can't optimize to that at compile time?
@Cruncher does my late edit satisfies as an answer?
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 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 (oracle.com/technetwork/articles/javase/devinsight-1-139780.html) - I think javac follows the same principle and creates dumb code.
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).

Comments

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.

1 Comment

To me, this argument sounds like you're claiming that a=a+4 is faster because it assumes correct types.