Java Compiler: Rearrange or Rewrite (Data, Software, Etc.) To Improve Efficiency of Retrieval or Processing

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Platform Independent means writing the code in one operating system (Windows XP) and executing it in another platform

(Linux)

WORA (Write Once and Run Anywhere) – Platform Independent

Bytecode – It is the machine language of the JVM

JVM – Converts the bytecode to executable code (.obj).

.obj – It is used to generate the output

Java Compiler: A Java compiler is a compiler for the programming language Java. The most common form of output from a Java compiler is Java
class files containing platform-neutral Java bytecode, but there are also compilers that gives optimized native machine code for a particular
hardware/operating system combination.

Most Java-to-bytecode compilers, Jikes being a well known exception, do virtually no optimization, leaving this until run time to be done by
the JRE

optimization - rearrange or rewrite (data, software, etc.) to improve efficiency of retrieval or processing.

Java virtual machine: The JVM loads the class files and either interprets the bytecode or just-in-time compiles it to machine code and then
possibly optimizes it using dynamic compilation.

Dynamic compilation is a process used by some programming language implementations to gain performance during program execution. Since
the machine code emitted (produced) by a dynamic compiler is constructed and optimized at program runtime, the use of dynamic compilation
enables optimizations for efficiency not available to compiled programs except through code duplication or metaprogramming.

In computer science, an interpreter is a computer program that directly executes, i.e. performs, instructions written in
a programming or scripting language, without previously compiling them into a machine language program. An interpreter generally uses one of
the following strategies for program execution:

1. parse the source code and perform its behavior directly.


2. translate source code into some efficient intermediate representation and immediately execute this.
3. explicitly execute stored precompiled code[1] made by a compiler which is part of the interpreter system.

Parse is the process of analysing (examining) a string of symbols, either in natural language or in computer languages, conforming to the rules of
a formal grammar. The term is used to refer to the formal analysis by a computer of a sentence or other string of words into its constituents
(part of something), resulting in a parse tree showing their syntactic (set of rules) relation to each other, which may also contain semantic and
other information.

In computing, source code is any collection of computer instructions, possibly with comments, written using a human-readable programming
language, usually as ordinary text

An Intermediate representation (IR) is the data structure or code used internally by a compiler or virtual machine to represent source code. An
IR is designed to be conducive for further processing, such as optimization and translation.

 Difference between Java Interpreter and JIT compiler in JVM?

a) Interpreter: Reads your source code or some intermediate representation (bytecode) of it, and executes it directly.
b) JIT compiler: Reads your source code, or more typically some intermediate representation (bytecode) of it, compiles that
on the fly and executes native code.

compiles that on the fly - Compiles the given bytecode instruction sequence to machine code at runtime before
executing it natively. It's main purpose is to do heavy optimizations (make something perfect) in performance

What does the JVM does?

 JVM is a virtual platform that resides on your RAM


 Its component, Class loader loads the .class file into the RAM
 The Byte code Verifier component in JVM checks if there are any access restriction violations in your code. (This is one of the principle
reasons why java is secure)
 Next, the Execution Engine component converts the Bytecode into executable machine code

Java Compiler - A Java compiler is a compiler for the programming language Java. The most common form of output from a Java compiler is Java
class files containing platform-neutral Java bytecode, but there are also compilers that gives optimized native machine code for a particular
hardware/operating system combination.

JIT Compiler – It is activated when a Java method is called. It converts the source code to native machine code just before running the program

JVM Architecture:

JVM is also an abstract machine (that does not have a physical existence, we cannot touch it.). It is a specification that provides the runtime
environment in which Java byte code can be executed. An instance of JVM is created, whenever you write Java command on the command
prompt to run the class.

Java Source code:


1. Java source code essentially consists of a main() method.
2. The main() method is public and thus can be called by any object. It is also static and so it can be called without instantiating the object
of the class.
3. The main() method does not return any value.
4. The controlling class of every Java program or application usually contains a main() method. This can be avoided to allow the class to be
tested in a stand-alone mode.
5. The other methods can subsequently be called in main() method.

Execution on JVM:
1. JVM executes “Java byte codes”, instead of executing source code directly.
2. The codes of other programming language can also be executed by JVM, if codes are converted to adequate Java byte codes.
3. Java Virtual Machine is different for different platforms and can also act as a platform itself.
4. JVM also supports the automatic error handling by intercepting the errors which can be controlled easily.
5. The above feature is useful in platform independency and multi-user ability of Java.

Note: The Java Compiler (Javac) reads the source code and converts it into byte code files known as class files.

JVM threads:
Basically JVM has two main threads, which are as following.

1. Demon thread

The Demon threads are being run by JVM for itself, and JVM only decides on a thread for being a demon thread. It is also used for
garbage collection.

2. Non-demon thread

Program’s main() is the initial and non-demon thread. The other implementations of threads are also known as non-demon threads. As
far as any non-demon thread is active, Java Virtual Machine is also active.
Architecture of JVM:

Now let’s start understanding each term one by one.

1. Class loader: It is a subsystem of Java Virtual Machine that is used to accept and load files.

Compilation creates class files.

2. Class or Method area: It stores per-class structure such as runtime constant pool, field and method data, the codes for methods, etc.

Interim memory is required during execution. It consists of heap, stack, registers to store data, etc.

3. Heap: It is the runtime data area in which objects are allocated.


4. Stack: Java stack stores frames. It also holds local variables and partial results, and plays a role in method invocation and return. Each
thread has a separate and private Java stack that is created at the same time as thread. Whenever a new method is invoked, a new
frame is created, and when the invocation completes the frame is destroyed.
5. Java native methods and libraries: It contains all the native methods and libraries used in the application.
6. Program counter (PC) register: It contains the address of the JVM instruction currently being executed.
7. Execution engine: It is the support of JRE or runtime environment and contains the following.

a) Interpreter that reads byte code stream and then execute the instructions.
b) A virtual processor i.e. JVM (Java Virtual Machine)
c) JIT (Just-In-Time) compiler which improves the performance by reducing the processing time.

You might also like