seminar 1: bytecodes - mit opencourseware...seminar 1: bytecodes author: weis, steve subject: java...
TRANSCRIPT
![Page 1: Seminar 1: Bytecodes - MIT OpenCourseWare...Seminar 1: Bytecodes Author: Weis, Steve Subject: Java Internationalization Tutorial Created Date: 12/14/2005 4:09:30 PM](https://reader033.vdocuments.mx/reader033/viewer/2022051809/60121ba6a224df64320a7437/html5/thumbnails/1.jpg)
Java BytecodeWhat’s inside class files.
MIT AITI July 2005
1
![Page 2: Seminar 1: Bytecodes - MIT OpenCourseWare...Seminar 1: Bytecodes Author: Weis, Steve Subject: Java Internationalization Tutorial Created Date: 12/14/2005 4:09:30 PM](https://reader033.vdocuments.mx/reader033/viewer/2022051809/60121ba6a224df64320a7437/html5/thumbnails/2.jpg)
©2005
MIT-Africa Internet Technology Initiative
What 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?
2
![Page 3: Seminar 1: Bytecodes - MIT OpenCourseWare...Seminar 1: Bytecodes Author: Weis, Steve Subject: Java Internationalization Tutorial Created Date: 12/14/2005 4:09:30 PM](https://reader033.vdocuments.mx/reader033/viewer/2022051809/60121ba6a224df64320a7437/html5/thumbnails/3.jpg)
©2005
MIT-Africa Internet Technology Initiative
Typical 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.
3
![Page 4: Seminar 1: Bytecodes - MIT OpenCourseWare...Seminar 1: Bytecodes Author: Weis, Steve Subject: Java Internationalization Tutorial Created Date: 12/14/2005 4:09:30 PM](https://reader033.vdocuments.mx/reader033/viewer/2022051809/60121ba6a224df64320a7437/html5/thumbnails/4.jpg)
Windows Source
BSD Port
PORTING COMPILATION EXECUTION
BSD Binary BSDMachine
Windows Machine
OS XMachine
LinuxMachine
Linux Binary
OS X Port
Linux Port
BSDCompiler
Windows Compiler
OS X Compiler
Linux Compiler
OS X Binary
Windows Binary
Figure by MIT OCW.4
![Page 5: Seminar 1: Bytecodes - MIT OpenCourseWare...Seminar 1: Bytecodes Author: Weis, Steve Subject: Java Internationalization Tutorial Created Date: 12/14/2005 4:09:30 PM](https://reader033.vdocuments.mx/reader033/viewer/2022051809/60121ba6a224df64320a7437/html5/thumbnails/5.jpg)
©2005
MIT-Africa Internet Technology Initiative
Write 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.
5
![Page 6: Seminar 1: Bytecodes - MIT OpenCourseWare...Seminar 1: Bytecodes Author: Weis, Steve Subject: Java Internationalization Tutorial Created Date: 12/14/2005 4:09:30 PM](https://reader033.vdocuments.mx/reader033/viewer/2022051809/60121ba6a224df64320a7437/html5/thumbnails/6.jpg)
©2005
MIT-Africa Internet Technology Initiative
Java 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.
6
![Page 7: Seminar 1: Bytecodes - MIT OpenCourseWare...Seminar 1: Bytecodes Author: Weis, Steve Subject: Java Internationalization Tutorial Created Date: 12/14/2005 4:09:30 PM](https://reader033.vdocuments.mx/reader033/viewer/2022051809/60121ba6a224df64320a7437/html5/thumbnails/7.jpg)
©2005
MIT-Africa Internet Technology Initiative
Using 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).
7
![Page 8: Seminar 1: Bytecodes - MIT OpenCourseWare...Seminar 1: Bytecodes Author: Weis, Steve Subject: Java Internationalization Tutorial Created Date: 12/14/2005 4:09:30 PM](https://reader033.vdocuments.mx/reader033/viewer/2022051809/60121ba6a224df64320a7437/html5/thumbnails/8.jpg)
Windows Source
BSD Port
PORTING COMPILATION EXECUTION
BSD Binary BSDMachine
Windows Machine
OS XMachine
LinuxMachine
Linux Binary
OS X Port
Linux Port
BSDCompiler
Windows Compiler
OS X Compiler
Linux Compiler
OS X Binary
Windows Binary
Figure by MIT OCW.8
![Page 9: Seminar 1: Bytecodes - MIT OpenCourseWare...Seminar 1: Bytecodes Author: Weis, Steve Subject: Java Internationalization Tutorial Created Date: 12/14/2005 4:09:30 PM](https://reader033.vdocuments.mx/reader033/viewer/2022051809/60121ba6a224df64320a7437/html5/thumbnails/9.jpg)
©2005
MIT-Africa Internet Technology Initiative
javap 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.
9
![Page 10: Seminar 1: Bytecodes - MIT OpenCourseWare...Seminar 1: Bytecodes Author: Weis, Steve Subject: Java Internationalization Tutorial Created Date: 12/14/2005 4:09:30 PM](https://reader033.vdocuments.mx/reader033/viewer/2022051809/60121ba6a224df64320a7437/html5/thumbnails/10.jpg)
©2005
MIT-Africa Internet Technology Initiative
javap -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.
10
![Page 11: Seminar 1: Bytecodes - MIT OpenCourseWare...Seminar 1: Bytecodes Author: Weis, Steve Subject: Java Internationalization Tutorial Created Date: 12/14/2005 4:09:30 PM](https://reader033.vdocuments.mx/reader033/viewer/2022051809/60121ba6a224df64320a7437/html5/thumbnails/11.jpg)
©2005
MIT-Africa Internet Technology Initiative
Example: String Appends
• Disassemble the following method usingjavap -c:
• public String append(String a, String b) {
return a+b;}
• The output is surprisingly complicated…
11
![Page 12: Seminar 1: Bytecodes - MIT OpenCourseWare...Seminar 1: Bytecodes Author: Weis, Steve Subject: Java Internationalization Tutorial Created Date: 12/14/2005 4:09:30 PM](https://reader033.vdocuments.mx/reader033/viewer/2022051809/60121ba6a224df64320a7437/html5/thumbnails/12.jpg)
public static java.lang.StringstringAdd(java.lang.String,java.lang.String,int);
Code:0: new #2; //class StringBuffer3: dup4: invokespecial #3; //Method
java/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}
12
![Page 13: Seminar 1: Bytecodes - MIT OpenCourseWare...Seminar 1: Bytecodes Author: Weis, Steve Subject: Java Internationalization Tutorial Created Date: 12/14/2005 4:09:30 PM](https://reader033.vdocuments.mx/reader033/viewer/2022051809/60121ba6a224df64320a7437/html5/thumbnails/13.jpg)
©2005
MIT-Africa Internet Technology Initiative
Example: 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.
13
![Page 14: Seminar 1: Bytecodes - MIT OpenCourseWare...Seminar 1: Bytecodes Author: Weis, Steve Subject: Java Internationalization Tutorial Created Date: 12/14/2005 4:09:30 PM](https://reader033.vdocuments.mx/reader033/viewer/2022051809/60121ba6a224df64320a7437/html5/thumbnails/14.jpg)
©2005
MIT-Africa Internet Technology Initiative
String 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.
14
![Page 15: Seminar 1: Bytecodes - MIT OpenCourseWare...Seminar 1: Bytecodes Author: Weis, Steve Subject: Java Internationalization Tutorial Created Date: 12/14/2005 4:09:30 PM](https://reader033.vdocuments.mx/reader033/viewer/2022051809/60121ba6a224df64320a7437/html5/thumbnails/15.jpg)
©2005
MIT-Africa Internet Technology Initiative
String 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 andn+1 method calls -- three times faster.
15
![Page 16: Seminar 1: Bytecodes - MIT OpenCourseWare...Seminar 1: Bytecodes Author: Weis, Steve Subject: Java Internationalization Tutorial Created Date: 12/14/2005 4:09:30 PM](https://reader033.vdocuments.mx/reader033/viewer/2022051809/60121ba6a224df64320a7437/html5/thumbnails/16.jpg)
©2005
MIT-Africa Internet Technology Initiative
Questions
• What advantages are there in runningprograms on simulated computers, likeJVMs?
• 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 sourcecode from class files, i.e. decompile?
16
![Page 17: Seminar 1: Bytecodes - MIT OpenCourseWare...Seminar 1: Bytecodes Author: Weis, Steve Subject: Java Internationalization Tutorial Created Date: 12/14/2005 4:09:30 PM](https://reader033.vdocuments.mx/reader033/viewer/2022051809/60121ba6a224df64320a7437/html5/thumbnails/17.jpg)
MIT OpenCourseWare http://ocw.mit.edu
EC.S01 Internet Technology in Local and Global Communities Spring 2005-Summer 2005
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.