Introduction

This post helps in having some basic understanding on JAVA before you even start analyzing a hello world program!!

  • Java is both a language and a platform.
  • Java programming language is case sensitive.
  • Java is half compiled half interpreted [we have a separate thread for this]
  • JVM (Java Virtual Machine) is not only for Java. There are 18+ languages that JVM executes.
  • Java is a portable language/ platform independent language – Because the byte code (compiled java code) generated in any machine can be ported to run anywhere in any other machine.

 Platform

 The simplest definition is available in Wikipedia, [http://en.wikipedia.org/wiki/Computing_platform]

1

Eg: computer architectureoperating system and Runtime libraries.

Virtual Machine [Reference: http://en.wikipedia.org/wiki/Virtual_machine]

Let me abstract out from what is clearly explained in Wikipedia. You must read at least this completely.

  • A machine that emulates the computer architecture and functions of a real time computer.
  • It is in fact a software implementation of a machine/computer.
  • An essential characteristic of a virtual machine is that the software running inside is limited to the resources and abstractions provided by the virtual machine—it cannot break out of its virtual environment.

System VM

  • Provides a complete platform that supports the execution of a complete OS.
  • Possibly categorized under hardware virtualization, this can mean the purpose of providing a platform to run programs where the real hardware is not available, or having multiple instances of VM’s leading to more efficient use of computing resources.

Process VM   [Also called as Language VM ,  Application virtual machine, or Managed Runtime Environment (MRE)]

  • Designed to run a single program. That means, it support a single process. A process is a program in execution.
  • Process VM is closely suited to one or more programming languages and built with the purpose of providing program portability
  • A process VM runs as a normal application inside a host OS and supports a single process. It is created when that process is started and destroyed when it exits.
  • Its purpose is to provide a platform-independent programming environment that abstracts away details of the underlying hardware or operating system, and allows a program to execute in the same way on any platform.

This type of VM has become popular with the Java programming language, which is implemented using the Java virtual machine.

Java source code (filename.java) is converted to byte code (filename.class) by the Java compiler, during the compilation process. The byte code is interpreted/translated to machine code by the JVM during execution stage.

JVM

  • Virtual machine that can execute java byte code [not just java byte code, many other byte codes like scala byte code].
  • The Java virtual machine (JVM) is a virtual “execution engine” instance that executes the bytecodes in Java class files on a microprocessor. How you tune your JVM affects the performance of WebLogic Server and your applications.
  • There are many programing languages based on JVM, and Java is just one among them.
  • The application launcher. JVM runs your program. JVM alone can’t run a java program. You need JRE.
  • The Java Virtual Machine provides a platform-independent way of executing code; That mean compile once in any machine and run it anywhere(any machine).
  • When we compile a Java file, output is not an ‘exe’ but it’s a ‘.class’ file. ‘.class’ file consists of Java byte codes which are understandable by JVM.
  • Java Virtual Machine interprets the byte code into the machine code depending upon the underlying operating system and hardware combination.
    (a)   Translate bytecode(.class files) to machine language. (b) Execute the machine language instructions

This also tells us that JVM is platform dependent, as it is dealing with machine instructions which depend on the underlying operating system and hardware combination.

  • Note that JVM is platform dependent. But it provides a platform independent running environment for your Java program [ your program does not care about the VM, correct? Yes! ]  [Read about Process Virtual Machine explained above, to understand this point]

JRE

  • JRE = JVM + others including runtime libraries. Hence JRE is also Platform dependent

JDK

  • JDK = JRE + others. Hence JDK is also Platform dependent

Java byte code is platform independent so Java classes and libraries are platform independent. As programmers we don’t have to worry about the platform, we write platform independent code, it is the job of runtime to call appropriate native code.

[ JVM is not distributed alone. It comes as a package along with JRE or JDK. ]

If you see the java download page, you will be asked about your platform (operating system, 32bit/64 bit etc).

  • JVM takes the byte code and translate to machine code.The byte code is both interpreted and Just in time compiled. Modern JVMs use a combination of interpretation and JIT techniques to get the benefit of both. The bytecode is first interpreted while the JIT is translating it in the background.

Interpretation and JIT compilation are two different strategies for executing bytecode

  • It is the JVM that makes Java programing language shout to the world that ‘I am portable, I am platform independent’

Kudos to JVM 🙂

Note:  JVM alone is not sufficient to run your java program, you need JRE which has the JVM and other runtime libraries.

Java Plug-in

Java Plug-in technology, included as part of the Java Runtime Environment, Standard Edition (Java SE), establishes a connection between popular browsers and the Java platform. This connection enables applets on Web sites to be run within a browser on the desktop.
Java Plug-in technology is part of the current version of the Java Runtime Environment, Standard Edition (Java SE). Download the JRE to get Java Plug-in.

Java Web Start   [Reference: http://docs.oracle.com/javase/tutorial/deployment/webstart/]

With Java Web Start software, users can launch a Java application by clicking a link in a web page. The link points to a Java Network Launch Protocol (JNLP) file, which instructs Java Web Start software to download, cache, and run the application.
Java Web Start software is installed as part of the JRE software.

You can run a Java Web Start application from a browser by clicking a link to the application’s JNLP file. The following text is an example of a link to a JNLP file.

<a href=”/some/path/Notepad.jnlp”>Launch Notepad Application</a>

Java Web Start software loads and runs the application based on instructions in the JNLP file. You can also start a Java Webstart program from desktop.
Follow http://docs.oracle.com/javase/tutorial/deployment/webstart/running.html

I clicked on a link to run a java webstart program, and end up downloading this file in my system! When I opened in a text editor, I saw this:

[sourcecode language=”java”] Notepad Demo Oracle Corporation [/sourcecode]

When I double clicked it, it ran just like a jar file, showing me  a notepad app.

 

JRE:

Java Runtime Environment. As the name suggests, it provides the environment to run java programs.

  • JRE provides libraries + JVM + other components to run applets,applications written in Java + javaPlugin + JavaWebstart
  • JRE is targeted for execution of a Java program.
  • JRE = JVM + Java Packages Classes (like util, math, lang, awt, swing etc.)+runtime libraries.
    ie: Java Runtime Environment contains JVM, class libraries, and other supporting files.

It does not contain any development tools such as compiler, debugger, etc.

  • Actually JVM runs the program, and it uses the class libraries, and other supporting files provided in JRE.

If you want to run any java program, you need to have JRE installed in the system.

JDK

Java software Development kit.

As the name says it is a kit that contains tools needed to develop the Java application.

This contains the java compiler, the Java executor (jre), and other tools that aid in java development & execution.
ie: JDK = superset of JRE.
Contains everything that is in JRE. + Tools such as compilers (javac.exe), Java application launcher (java.exe), debuggers necessary for developing applications.

  • OpenJDK and Oracle JDK.

Oracle is not the only provider to provide JDK

  • OpenJDK – The Open-source implementation of the Java SE 7 JSR
  • Oracle JDK – Oracle’s implementation of the Java SE 7 JSR
  • Compiler converts java code [Test.java] into byte code[Test.class].
  • Java application launcher opens a JRE, loads the class, and invokes its main method in a main thread.
  • You might need JDK, if at all you want to write your own programs, and to compile them.
  • For running java programs, JRE is sufficient.

 

Difference between JVM and HotSpot?

Long story short, JVM is a specification and Hotspot is Oracle’s implementation of JVM

  • The JVM is by definition a virtual machine, i. e. a software machine that simulates what a real machine does.
    Like a real machine, it has an instruction set, a virtual computer architecture and an execution model. It is capable of running code written with this virtual instruction set, pretty much like a real machine can run machine code.
  • HotSpot is an an implementation of the JVM concept.
  • It was originally developed by Sun and now it is owned by Oracle.
  • There are other implementations of the JVM specification, like JRockitIBM J9, among many others.
  • The OpenJDK is a project under which an opensource implementation of HotSpot (and many other pieces of the JDK e.g compiler, APIs, tools, etc) is developed.

Detailing,

  • The Java HotSpot Virtual Machine is a core component of the Java SE platform.
  • It implements the Java Virtual Machine Specification
  • It is delivered as a shared library in the Java Runtime Environment.
  • As the Java bytecode execution engine, it provides Java runtime facilities, such as thread and object synchronization, on a variety of operating systems and architectures.
  • It includes dynamic compilers that adaptively compile Java bytecodes into optimized machine instructions and efficiently manages the Java heap using garbage collectors, optimized for both low pause time and throughput.
  • It provides data and information to profiling, monitoring and debugging tools and applications.
  • HotSpot is an “ergonomic” JVM.
  • Based upon the platform configuration,
    it will select
    –   a compiler,
    –   Java heap configuration, and
    –   garbage collector that produce good to excellent performance for most applications.
  • Under special circumstances, however, specific tuning may be required to get the best possible performance.

 

JCP

Java Community Process.

Quoting from Wikipedia, JCP is a formalized mechanism that allows interested parties to develop standard technical specifications for Java technology. The JCP involves the use of JSRs.

JSR

Java Specification request.

Quoting from Wikipedia, JSRs are the formal documents that describe proposed specifications and technologies for adding to the Java platform.