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
- It should be "simple, object-oriented and familiar"
- It should be "robust and secure"
- It should be "architecture-neutral and portable"
- It should execute with "high performance"
- 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
Programmer writes program using an editor; stores program on disk with the .java file name extension
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
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
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.
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!...