Monday, July 14, 2014

How Java program works

How Java program works
Java is a computer programming language that is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible. It is intended to let application developers "write once, run anywhere" (WORA), meaning that code that runs on one platform does not need to be recompiled to run on another. Java applications are typically compiled to bytecode (class file) that can run on any Java virtual machine (JVM) regardless of computer architecture. In this post, we'll see the basic five phase of Java program life cycle.

Virtual Machine
The heart of the Java platform is the concept of a "virtual machine" that executes Java bytecode programs. This bytecode is the same no matter what hardware or operating system the program is running under. There is a JIT (Just In Time) compiler within the Java Virtual Machine, or JVM. The JIT compiler translates the Java bytecode into native processor instructions at run-time and caches the native code in memory during execution.
The use of bytecode as an intermediate language permits Java programs to run on any platform that has a virtual machine available. The use of a JIT compiler means that Java applications, after a short delay during loading and once they have "warmed up" by being all or mostly JIT-compiled, tend to run about as fast as native programs.


Primary goals in the creation of the Java language
  1. It should be "simple, object-oriented and familiar"
  2. It should be "robust and secure"
  3. It should be "architecture-neutral and portable"
  4. It should execute with "high performance"
  5. It should be "interpreted, threaded, and dynamic"

Types of Java
There are many types of Java programs which run differently:
  • Java Applet - is usually stored on a website and is downloaded and run on a client computer from within a web browser
  • Application - can only be run on the computer. If online, it has to be downloaded before being run.
  • JAR File - is used to package Java files together into a single file. (Almost exactly like a .zip file.)
  • Servlet - runs on a web server and helps to display web pages.
  • Swing application - is used to build an application that has a GUI (windows, buttons, menus, etc.)
  • EJB - runs on a web server and is used to develop large, complex websites


Java programs go through five phases

Edit
Programmer writes program using an editor; stores program on disk with the .java file name extension

Compile
Use javac (the Java compiler) to create bytecodes from source code program; bytecodes stored in .class files. To compile a program called Welcome.java, type
javac Welcome.java


Load 
Class loader reads bytecodes from .class files into memory.
The program must first be placed in memory before it can be executed. This is  done by the class loader, which takes the .class file (or files) containing the bytecodes and transfers it to memory. The .class file can be loaded from a disk on your system or  over a network

Verify
Bytecode verifier examines bytecodes to ensure that they are valid and do not violate security restrictions.

The JVM verifies all bytecode before it is executed. This verification consists primarily of three types of checks:

  • Branches are always to valid locations
  • Data is always initialized and references are always type-safe
  • Access to private or package private data and methods is rigidly controlled

The first two of these checks take place primarily during the verification step that occurs when a class is loaded and made eligible for use. The third is primarily performed dynamically, when data items or methods of a class are first accessed by another class.
The bytecode verifier traverses the bytecodes, constructs the type state information, and verifies the types of the parameters to all the bytecode instructions.



Execute
Java Virtual Machine (JVM) uses a combination of interpretation and just- in-time compilation to translate bytecodes into machine language

Summary of above phases in picture


Through the Java VM , the same application is capable of running on multiple platforms.






If you know anyone who has started learning Java, why not help them out! Just share this post with them. Thanks for studying today!...

4 comments:

  1. Good job pardeep kumar easy way to underdsand for beginners who are studying java for the first time

    ReplyDelete
  2. Hi Pradeep Kumar Ji,Nice Presentation.Thanks a LOT for the information provided.Very Valuable for Freshers.Please continue with your Great Work.May GOD BLESS YOU...

    ReplyDelete
  3. can you help me to understand b/w java complier and jit compiler and their role?

    ReplyDelete