Monday, November 4, 2013

Java Virtual Machine - Introduction

Typical Java Environment consists of following:
  •  Java Programming Language
    • JDK
    • Source code is in Java.
  • Java Class File Format
    • Bytecode
  • Java API
    • Standard classes and interfaces of JDK.
  • JVM
    • Java Virtual Machine

JVM:

  • The Java Virtual Machine, or JVM, is code executing component of Java platform.
  • Abstract machine that runs compiled Java programs.
  • The JVM is "virtual" because it is implemented in software on top of a "real" hardware platform and operating system.
  • The Java Virtual Machine is an abstract computing machine. Like a real computing machine, it has an instruction set and manipulates various memory areas at run time. 
  • Oracle JVM specifications specifies an abstract machine, it doesn't describe any particular implementation of JVM.
  • Java is for platform-independence, each Java platform has different constraints. Java specification doesn’t give hard and fast rules about the design of JVM with respect to memory and other aspects. JVM is implemented specifically for each platform, and implementation of JVM is left to jvm implementers.

Role:

  • Read the Class file format (Bytecode) and correctly performs the operations specified in it.

Key points:

  • Any implementation details like memory structures, garbage-collection etc are not part of the Java Virtual Machine's specification would unnecessarily constrain the creativity of implementers.
  • The Java Virtual Machine knows nothing of the Java programming language, it only cares for Virtual Machine instructions (or bytecodes).
  • All Java Virtual Machine implementations must recognize the Java class file format, but individual implementations may also recognize other binary formats.
  • Any language with functionality that can be expressed in terms of a valid class file can be hosted by the Java Virtual Machine.
  • For the sake of security, the Java Virtual Machine imposes strong syntactic and structural constraints on the code in a class file for security concerns.

Advantages:  

  • Hardware and operating system independence. 
  • Small size of its compiled code. 
  • Protect users from malicious programs. 
    • Bytecode is carefully inspected by JVM before execution.

JVM Implementation:

  • Like most virtual machines, the Java virtual machine machine has a stack-based architecture akin to a microcontroller/microprocessor. 
  • JVM doesn't have registers for storing arbitrary values. All computation in the JVM centers on the stack, everything must be pushed onto the stack before it can be used in a calculation.
  • JVM are usually coded for a particular OS but it can also be coded directly on hardware.
  • JVM also has low level support for Java like classes and methods, which amounts to a highly idiosyncratic memory model and capability-based architecture.
  • JVM has no built-in support for dynamically typed languages.
  • JVM supports following 7 primitive types:
    • 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-b yte signed two's complement integer
    • float     -    4-byte IEEE 754 single-precision float
    • double -    8-byte IEEE 754 double-precision float
    • char     -    2-byte unsigned Unicode character
  • Few Examples:
    • Most popular JVM implementation is from Oracle and is named HotSpot, written in C++.
    • Jinitiator is another popular JVM implementation, especially in Oracle Apps community, developed by Oracle for supporting forms on websites before they purchased Sun. 
    • Mac OS Runtime for Java (MRJ, originally Macintosh Runtime for Java).
    • SAPJVM (SAP) is a licensed and modified SUN JVM ported to all supported platforms of SAP NetWeaver

About Bytecode

  • Machine language of the Java virtual machine.
  • Each instruction consists of a one-byte opcode(represents operation) followed by zero or more operands.
  • An opcode may optionally be followed by operands(represents parameters required by opcodes).
  • The total number of opcodes is less (~200), each opcodes requires only one byte. 
    • This helps in minimizing the size of class files and JVM implementation.
  • Java Bytecode is an intermediate language which is typically compiled from Java but it can also be compiled from other programming language, ex ADA.
  • Class files contain bytecode and a symbol table, as well as other ancillary information.
  • Any language with functionality that can be expressed in terms of a valid class file can be hosted by the Java Virtual Machine.
  • JVM has instructions for Load/Store of variables, Arithmetic, Type Conversions, Stack Management, Exception management, and Monitor based concurrency control etc.
  • Examples of Opcodes:
    • iload, lload, fload, and dload  are used to load int, long, float and double operand on the stack respectively.
    • istore, fstore are used to pop int and float from the stack to a variable.
    • aload, astore operates on references.
  • There is no support for loops and binary logical operators (|| &&). These are implemented by compiler with jump instructions.

Class File

  • Each class file contains the definition of a single class or interface. 
  • A class file consists of a stream of 8-bit bytes. All 16-bit, 32-bit, and 64-bit quantities are constructed by reading in two, four, and eight consecutive 8-bit bytes, respectively. 
  • Multibyte data items are always stored in big-endian.

Execution Engine:

  • It consists of following:
    • Bytecode Interpreter
    • JIT (Just In Time) Compiler
      • Since byte code is interpreted it executes slower than compiled machine code.
      • Just-in-time compilation (JIT) is also known as dynamic translation.
      • JIT is used to improve the runtime performance of byte code.
      • The program is stored in memory as byte code, but the code segment currently running is preparatively compiled to physical machine code in order to run faster.
    • Virtual Processor

JRE:

  • Java Execution Environment (Java Runtime Environment). 
  • JRE contained JVM and java class libraries that implement java API.

Other JVM Languages

  • Groovy
  • Scala
  • Jython
  • JRuby
  • Kotlin

No comments:

Post a Comment