Monday, December 29, 2008

What is Java?

  • Java generally refers to a combination of three things
    • the Java programming language
    • the Java Virtual machine
    • the Java platform
  • Java the language is a high-level OOP language, influenced in various ways by C, C++ and Smalltalk
  • Java has been around officially since 1996
  • Java has evolved tremendously all through these years and has spawned a number of technologies like
    • Servlet technology
    • component technology like JavaBeans
    • JavaServerFaces
    • and a whole host of other technologies and tools
  • Its syntax was designed to be familiar to those familiar with C-descended "curly brace" languages, but with stronger OO principiles than those found in C++
  • One of Java's more controversial aspects is its incomplete object-orientation - Java primitives such as int, char, boolean etc. are not objects
  • The inclusion of primitives increases Java performance, but at arguably expense of code clarity
  • Java 5.0 introduces an "autoboxing" scheme to eliminate many uses of the wrapper classes, but in some ways it obsures what is really going on
  • Java is a fail early language - because of its syntatic restrictions, many programming failures are simply not possible in Java
  • With no direct access to pointers, pointer-arithmetic errors are non-existent
  • Using an object as a different type than what it was originally declared to be requires an explicit cast, which gives the compiler an opportunity to reject illogical programming, like calling a String method on an Image
  • Java is generally understood to be the most popular general-purpose computing language in use today

  • Java is generally thought of in terms of three platforms
    • Standard Edition(SE)
    • Enterprise Edition (EE)
    • Micro Edition (ME)
  • Each describes the combination of a language version, a set of standard libraries, and a virtual machine to execute the code
  • EE is a superset of SE - any EE application can assume the existence of all of the SE libraries
  • EE's use of the language is identical to SE's
  • Because of the limitations of small devices like phones and set-top boxes, Java ME differs significantly from its siblings
  • It is not a subset of SE
  • ME eliminates some language features, such as the float primitive and Float class, reflecting the computing limitations of the platforms it runs on

Java Runtime Environment
  • Java Runtime Environment is a subset of JDK
  • It is an implementation of the JVM which actually executes Java programs
  • JRE is a plug-in needed for running Java programs
  • JRE includes JVM, core libraries and other additional components to run applications and applets written in Java


Java Virtual Machine
  • One of the strong points in favor of Java is "Write Once, Run Anywhere"
  • Java is platform-independent
  • Before Java, programmers would have to write the program for the Windows OS and again for the Mac OS
  • Java eliminates this problem with the use of JVM
  • Java Virtual Machine as its name suggests is a "virtual" computer that resides in the real computer as a software process
  • The JVM essentially runs between the computer and the Java program
  • JVM gives Java the flexibility of platform independence
  • At some point, Java source needs to become platform-native executable code
  • This typically requires a two step process - the developer compiles the source into Java bytecode, and then a JVM converts this into native code for the host platform
  • Java code is written in a ".java" file
  • This code contains one or more Java language attributes like Class, Methods, Variables, Objects etc.
  • Javac is used to compile this code and to generate a ".class" file
  • Class file is also known as byte code
  • java byte code is an input to JVM
  • JVM reads this code and interprets it and executes the program
  • JVM helps the JRE to run Java applications
  • JVM operates on Java bytecode, which is normally generated from Java source code
  • A JVM can also be used to implement programming languages other than Java
  • For example, Ada source code can be compiled to Java bytecode, which may then be executed by a JVM
  • JVMs can also be released by other companies besides Sun - JVMs using the "Java" trademark may be developed by other companies as long as they adhere to the JVM specification published by Sun



Components of JVM
  • JVM is divided into several components like the stack, the garbage-collected heap, the registers and the method area
  • Stack in JVM stores various method arguments as well as the local variables of any method
  • Stack also keeps track of each and every method invocation - called the Stack Frame
  • There are three registers that helps in stack manipulation - vars, frame and optop
  • These registers point to different parts of current stack
  • There are three sections in Java stack frame
    • Local Variables - contains all the local variables being used by the current method invocation. It is pointed to by the vars register
    • Execution Environment - used to maintain the operations of the stack itself. It is pointed to by the frame register
    • Operand Stack - used as a work space by bytecode instructions. It is here that the parameters for bytecode instructions are placed, and the results of bytecode instructions are found. The top of the operand stack is pointed to by the optop register
  • Method Area - is the area where bytecode resides
  • The program counter points to some byte in the method area
  • It always keeps track of the current instruction which is being executed
  • After execution of an instruction, the JVM sets the PC to next instruction
  • Method area is shared among all the threads of a process
  • If more than one thread is accessing any specific method or instruction, synchronization is required
  • Synchronization in JVM is achieved through Monitors
  • Garbage-Collected Heap - is where the objects in Java programs are stored
  • Whenever an object is created using the new operator, the heap comes into picture and memory is allocated from there
  • unlike C++, Java doesn't have free operator to free any previously allocated memory
  • Java does this automatically using Garbage collection mechanism
  • "Mark and Sweep" algorithm is used as garbage collection logic
  • The local object reference resides on Stack, but the actual object resides in Heap
  • Arrays in Java are objects, hence they also reside in Garbage-Collected Heap


(Compiled from http://viralpatel.net/blogs/2008/12/java-virtual-machine-an-inside-story.html, Beyond Java and Widipedia)

No comments: