java byte code & virtual machine

Post on 06-May-2015

501 Views

Category:

Software

4 Downloads

Preview:

Click to see full reader

DESCRIPTION

As per S.Y.BSc.(Computer Science) Syballus

TRANSCRIPT

JAVA BYTE CODE &

JAVA VIRTUAL MACHINE(JVM)GROUP MEMBERS :-

07. ANKITA14. ASHUTOSH

18. SHAMIKSHA19. LAXMAN

JAVA BYTECODE Byte codes are the machine language of the

Java virtual machine. When a JVM loads a class file, it gets one

stream of byte codes for each method in the class.

 The byte codes streams are stored in the method area of the JVM.  

Byte code, also known as p-code (portable code), is a form of instruction set designed for efficient execution by a software interpreter.

A method's byte code stream is a sequence of instructions for the Java virtual machine.

Each instruction consists of a one-byte op code followed by zero or more operands.

The op code indicates the action to take. If more information is required before the JVM can take the action, that information is encoded into one or more operands that immediately follow the opcode.

Each type of opcode has a mnemonic. In the typical assembly language style, streams of Java bytecodes can be represented by their mnemonics followed by any operand values. For example, the following stream of bytecodes can be disassembled into mnemonics:

// Bytecode stream: 03 3b 84 00 01 1a 05 68 3b a7 ff f9

Disassembly: iconst_0 // 03 istore_0 // 3b iinc 0, 1 // 84 00 01 iload_0 // 1a iconst_2 // 05 imul // 68 istore_0 // 3b goto -7 // a7 ff f9

The seven primitive types are listed in the following table:

TYPE DFEFINITION

byte one-byte signed two's complement integer.

short two-byte signed two's complement integer.

int 4-byte signed two's complement integer.

long 8-byte signed two's complement integer.

float 4-byte IEEE 754 single-precision float.

double 8-byte IEEE 754 double-precision float.

What is in a .class file?Java class file is a file (with the .class extension)

containing a Java byte code which can be executed on the Java Virtual Machine (JVM). Java class file is produced by Java compiler from Java programming language source files (.java files) containing Java classes. If a source file has more than one class, each class is compiled into a separate class file

In Short, Create source file Compile source file into .class file Run the .class file

8

The Interpreter's are sometimes referred to as the Java Virtual Machines

The output of the compiler is .class file

Stack-based virtual machine

Small instruction set: 202 instructions (all are 1 byte opcode + operands)Intel x86: ~280 instructions (1 to 17 bytes long!)

Every Java class file begins with magic number 3405691582 = 0xCAFEBABE

JVM executes the step-by-step instructions given to it from the bytecode

JVM is specific to each computer architecture (Linux JVM, Windows JVM, Mac JVM).

Java Byte code

Example :

1 + 2 1 2 +

PUSH 1 PUSH 2 ADD

3

+

1 2

1 + 2 1 2 +

ICONST_1 ICONST_2 IADD

3

+

1 2

JAVA JAVAP TOOLThe javap tool disassembles compiled Java™

files and prints out a representation of the Java program. This may be helpful when the original source code is no longer available on a system.

Link to refer about javap !!!http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javap.html

Execution EnvironmentOracle's Java execution environment is termed

the Java Runtime Environment, or JRE.

The execution environment section is used to maintain the operations of the stack itself. It is pointed to by the frame register.

The JVM runtime executes .class or .jar files, emulating the JVM instruction set by interpreting it or using a just-in-time compiler (JIT) such as Oracle's HotSpot.

Runtime Data Areas

PC register: PC register has the address of a JVM instruction being executed now.

JVM stack: One JVM stack exists for one thread, and is created when the thread starts. It is a stack that saves the struct (Stack Frame).

Native method stack: A stack for native code written in a language other than Java.

Method area: It stores runtime constant pool, field and method information, static variable, and method byte code for each of the classes and interfaces read by the JVM.

Runtime constant pool: This area is included in the method area; however, it plays the most core role in JVM operation. Therefore, the JVM specification separately describes its importance. As well as the constant of each class and interface, it contains all references for methods and fields.

Heap: A space that stores instances or objects, and is a target of garbage collection.

Runtime data areas shared among all threads.

Runtime data areas exclusive to each thread.

Execution EngineThe bytecode that is assigned to the runtime data areas in the JVM via class loader is executed by the execution engine.

The execution engine reads the Java Bytecode in the unit of instruction.

The execution engine gets one OpCode and execute task with the Operand, and then executes the next OpCode.

Features of JVMStack-based virtual machine:

The most popular computer architectures such as Intel x86 Architecture and ARM Architecture run based on a register.

Symbolic reference: All types (class and interface) except for primitive data types are referred to through symbolic reference, instead of through explicit memory address-based reference. 

Garbage collection: A class instance is explicitly created by the user code

Guarantees platform independence by clearly defining the primitive data type: A traditional language such as C/C++ has different int type size according to platform.

Network byte order: The Java class file uses the network byte order.

What is VM ?The purpose of VM is to

abstract the details of the hardware or the operating system and to let a program execute as same way on any operating system.

Can think VM as type of computer. It is not new idea

The JVM is most widely used machine

Source code

VM compiler

VM code

VM interpreter

Executable code

Java code(.java)

Javac compiler

Byte code (.class)

WindowsJVM

LinuxJVM

Mac JVM

Windows Linux

Mac

What is VM as it applies to JAVA?A very large installed

base, this computer is efficient, dynamic and net aware.

A JVM facilitates Write-Once, Run-Run Anywhere (WORA)

It is not new idea

The JVM is most widely used machine

JVM has various sub components internally.

1. Class loader sub system: JVM's class loader sub system performs 3 tasks      a. It loads .class file into memory.      b. It verifies byte code instructions.      c. It allots memory required for the program.

2. Run time data area: This is the memory resource used by JVM and it is divided into 5 part

  a. Method area: Method area stores class code and method code.

      b. Heap: Objects are created on heap.

      c. Java stacks: Java stacks are the places where the Java methods are executed. A Java stack contains frames. On each frame, a separate method is executed.

      d. Program counter registers: The program counter registers store memory address of the instruction to be executed by the micro processor.

      e. Native method stacks: The native method stacks are places where native methods (for example, C language programs) are executed. Native method is a function, which is written in another language other than Java.

3. Native method interface: Native method interface is a program that connects native methods libraries (C header files) with JVM for executing native methods.

4. Native method library: holds the native libraries information.

5. Execution engine: Execution engine contains interpreter and JIT compiler, which covert byte code into machine code. JVM uses optimization technique to decide which part to be interpreted and which part to be used with JIT compiler.

top related