wood burning stoves 2.0*
The moose likes Performance and the fly likes assignment operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "assignment operators" Watch "assignment operators" New topic
Author

assignment operators

Rob MacKinnon
Greenhorn

Joined: Dec 17, 2001
Posts: 4
Will useing a assignment operator reduce the bite-code size.
ex. will x += y giva a smaller code than x = x + y?
Peter Haggar
author
Ranch Hand

Joined: Jan 03, 2001
Posts: 106
The bytecode size is the same, although the bytecode is slightly different. However, the difference is not going to amount to much, if anything. In addition, if you run with a JIT, it will most likely be compiled into the same instructions. Using the IBM JDK 1.3 with its JIT, I got the following bytecode and assembler:

produced this bytecode:

Note they are the same length, but the opcode at location 1 is different. x = x + y; uses iload_0 while x+=y; uses a dup.
You're unlikely to see any difference even if you are running interpreted. Since you probably are not, it's more important to see what the JIT produces. Both method's foo and bar produced the same assembler with the IBM JIT:

Peter Haggar
------------------
Senior Software Engineer, IBM
author of: Practical Java


Senior Software Engineer, IBM
author of: Practical Java
Horaci Macias
Ranch Hand

Joined: Nov 08, 2001
Posts: 74
Could you tell me how to see the assembler produced by a java code ?
Thank you,
Horaci Macias
Peter Haggar
author
Ranch Hand

Joined: Jan 03, 2001
Posts: 106
Originally posted by Horaci Macias:
Could you tell me how to see the assembler produced by a java code ?

The bytecode is produced by using the javap tool. On a class file named test.class this works on Windows:
javap -c test > test.bc
To see the assembler, the JVM you run with must have a JIT (most do). I took the code above and wrapped it in an infinite loop. I then ran the program and debugged the java process with the Microsoft Visual C++ debugger. When the debugger starts, I break the execution of the program. Since the code I wanted to see is spinning in an infinite loop, it's quite easy to see that code as you step through the assembler.
Peter Haggar
------------------
Senior Software Engineer, IBM
author of: Practical Java
 
Consider Paul's rocket mass heater.
 
subject: assignment operators