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
Could you tell me how to see the assembler produced by a java code ? Thank you, Horaci Macias
Joined: Jan 03, 2001
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