Unformatted text preview:

Java BytecodeWhat are these .class files?Typical Software ProductionWrite Once, Run AnywhereJava Virtual MachinesUsing javapjavap Outputjavap -c OutputExample: String AppendsExample: String AppendsString Append OptimizationString Append OptimizationQuestionsJava BytecodeWhat’s inside class files.MIT AITI July 2005©2005MIT-Africa Internet Technology InitiativeWhat are these .class files?• You’ve created .java source code files.• You’ve compiled .java source code into .class files.• You’ve run your .class files.• But what’s are those .class files?©2005MIT-Africa Internet Technology InitiativeTypical Software Production• Source code is ported to different platform-specific sources.• Each port is compiled with a platform-specific compiler.• Each compiler produces platform-specific machine code (or binaries).• Binaries execute on a single platform.Windows SourceBSD PortPORTING COMPILATION EXECUTIONBSD BinaryBSDMachineWindows MachineOS XMachineLinuxMachineLinux BinaryOS X PortLinux PortBSDCompilerWindows CompilerOS X CompilerLinux CompilerOS X BinaryWindows BinaryFigure by MIT OCW.©2005MIT-Africa Internet Technology InitiativeWrite Once, Run Anywhere• Java source code is is compiled into machine-independent bytecode class files. • javac command compiles .java source code into .class bytecode.• Bytecode is interpreted by machine-specific Java Virtual Machines (JVMs).• Bytecode consists of simple, step-by-step instructions for the JVM.©2005MIT-Africa Internet Technology InitiativeJava Virtual Machines• JVM is a computer simulated in a computer. • JVMs are built into most web browsers.• java command gives .class file to JVM. • JVM interprets the bytecode into instructions that a specific platform can understand.• Different JVMs for different platforms: Windows, Mac OS X, Linux, cell phones.©2005MIT-Africa Internet Technology InitiativeUsing javap• javap is a program that can give you information on .class files.• If you get a class file without documentation, javap can tell you what methods you can call.• javap can show you how javac compiles your program into simple instructions.• Good for high-performance optimizations or if you don’t have access to an Application Programming Interface (API).Windows SourceBSD PortPORTING COMPILATION EXECUTIONBSD BinaryBSDMachineWindows MachineOS XMachineLinuxMachineLinux BinaryOS X PortLinux PortBSDCompilerWindows CompilerOS X CompilerLinux CompilerOS X BinaryWindows BinaryFigure by MIT OCW.©2005MIT-Africa Internet Technology Initiativejavap Output• Given “Mystery.class”:> javap MysteryCompiled from "Mystery.java"class Mystery extends java.lang.Object{Mystery();public static int unknown(int,int);}• One static method called “unknown” that takes two integers and returns an integer.©2005MIT-Africa Internet Technology Initiativejavap -c Output• Using the “-c” flag will disassemble the bytecode:public static int unknown(int,int);Code:0: iload_01: iload_12: iadd3: ireturn}Loads the first integer argument.Loads the second integer argument.Adds the two loaded integer values.Returns the integer sum.This method just adds two numbers together and returns the sum.©2005MIT-Africa Internet Technology InitiativeExample: String Appends• Disassemble the following method usingjavap -c:• public String append(String a, String b) {return a+b;}• The output is surprisingly complicated…public static java.lang.StringstringAdd(java.lang.String,java.lang.String,int);Code:0: new #2; //class StringBuffer3: dup4: invokespecial #3; //Methodjava/lang/StringBuffer."<init>":()V7: aload_08: invokevirtual #4; //Method java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;11: aload_112: invokevirtual #4; //Method java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;15: invokevirtual #5; //Method java/lang/StringBuffer.toString:()Ljava/lang/String;18: areturn}©2005MIT-Africa Internet Technology InitiativeExample: String Appends• All that bytecode is equivalent to:StringBuffer temp;temp = new StringBuffer();temp.append(a);temp.append(b);return temp.toString();• One line of code initializes an object and makes three method calls.©2005MIT-Africa Internet Technology InitiativeString Append Optimization• What if we had:• String a =“”; String b = “hello”;for (int i=0; i<n; i++)a += b;• This code performs n initializations and 3n method calls.• Using javap showed us there’s a class called StringBuffer that has an append() function.©2005MIT-Africa Internet Technology InitiativeString Append Optimization• A more efficient way:StringBuffer aBuff = new StringBuffer();String b = “hello”;for (int i=0; i<n; i++) a.append(b);String a = aBuff.toString();• Only performs two initializations and n+1 method calls -- three times faster.©2005MIT-Africa Internet Technology InitiativeQuestions• What advantages are there in running programs on simulated computers, like JVMs?• What disadvantages might there be?• Could you compile Java directly into native machine code like C or C++, instead of using a JVM?• Is it possible to generate Java source code from class files, i.e.


View Full Document

MIT SP 772 - Java Bytecode

Download Java Bytecode
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java Bytecode and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java Bytecode 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?