1 / 50

Chapter 16

Java Virtual Machine. Chapter 16. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode (machine language for the Java Virtual Machine (JVM). To run Simple.class, enter java Simple.

trevor
Download Presentation

Chapter 16

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Virtual Machine Chapter 16

  2. To compile a java program in Simple.java, enter javac Simple.javajavac outputs Simple.class, a file that contains bytecode (machine language for the Java Virtual Machine (JVM).To run Simple.class, enterjava Simple

  3. java (the Java interpreter) makes your computer act like the JVM so it can execute bytecode.

  4. Why is Java slow? • Interpretation of bytecode can involve a lot of overhead. • JVM dynamically links classes. • JVM performs checks during loading, linking, and executing bytecode.

  5. Why is Java good for the Web? • Bytecode is space efficient. • Bytecode is portable to any system with a java interpreter. • Java applets are safe to run.

  6. Four parts of the JVM • Execution engine (contains pc register) • Method area (contains information on each class: bytecode, static variables, information needed for verification and linking). • Java stack (the run time stack). Each frame of the Java stack contains a local variable array and an operand stack. • heap (contains data associated with objects). Periodically, garbage collection deallocates objects in the heap that are no longer referenced.

  7. There are two types of stacks in the JVM • The Java stack • The Java stack consists of frames, one frame for each method invocation. Each frame contains an operand stack and a local variable array.

  8. Contains local variables numbered starting from 0. For example, the first slot of the local variable array is called local variable 0. Local variable array

  9. Used to hold operands and results during the execution of instructions. Operand stack

  10. Some instructions consist of an opcode only. For example,iconst_0, iconst_1, iconst_2, iconst_3, iconst_4, iconst_5which push 0, 1, 2, 3, 4, and 5, respectively, onto the operand stack.The more common operations are performed by such opcode-only instructions.

  11. Some instructions require an operand. For example, bipush 6which pushes 6. This instruction consists of the opcode for bipush followed by a byte containing 6.To push a number greater than 127, use sipush (short int push). For example, sipush 130

  12. Symbolic bytecode that adds three numbers

  13. a: reference d: double f: float i: integer ia: integer array l: long The initial letter of some mnemonics indicates the data type. For example, iadd, dadd, fadd, ladd.

  14. Load instructions on the JVM iload_0pushes the value in local variable 0 (i.e., it pushes the value from the first slot of the local variable array onto the operand stack.) iload 4pushes the value in local variable 4.

  15. Store instructions on the JVM istore_0pops and stores the value on top of the operand stack into local variable 0. istore 4pops and stores the value on top of the operand stack into local variable 4.

  16. A static variable in Java is a variable associated with a class rather than an object. It is shared by all objects of its class.A static method in Java is a method that can be called via its class.

  17. The getstatic and putstatic instructions transfervalues between the top of the operand stack and static variables.The operand that appears in getstatic and putstatic instructions is an index into the constant pool. For example,getstatic 22 is a constant pool index.

  18. Invoking a static method with invokestatic instruction • Creates frame for the called method and pushes it onto the Java stack. • Pops the arguments from the caller’s operand stack and places them in the called method’s local variable array starting from local variable 0. • Transfers control to the called method.

  19. Returning a value to the calling method with the ireturn instruction The value returned is pushed onto the calling method’s operand stack.

  20. Implementation of the execution engine

  21. The wisdom of using a stack architecture for the JVM • A stack architecture on a simulated machine is no slower than a register architecture. • Bytecode is very compact which is important for a web programs.

  22. A simple Java program follows, along with its bytecode

  23. A formatted display of the constant pool for our simple program follows.

  24. Information in the constant pool for index 3

  25. An attribute in a class file The first entry is the constant pool index of the attribute name. The second entry is the length of what follows.

  26. A hex display of the complete class file for our simple program follows.

  27. Sizes of comparable programs

  28. Some comparison and control instructions • goto unconditional jump • if_cmplt compares top two stack items • if_icmpge compares top two stack items • iflt compares top of stack with 0 • if_acmpeq compares references • if_acmpne compares references See the illustrative program on the next slide.

  29. A70006 (the machine code for goto 6) jumps to the location whose address is 6 + the contents of the pc register (before incrementation). Instructions that jump use pc-relative addressing

  30. javap –c Simple Unassembling the Simple class file

  31. Unassemble this program to see its bytecode

  32. Arrays and objects

  33. Now that you know the basics of the JVM, you can enjoy (and understand) some more advanced discussions of the JVM.

  34. return 0;

More Related