Java Preview
Java Preview
Java Preview
By Krishna Rungta
Chapter 38: Interesting trick with For Loop – Using the Foreach loop in Java
What is PC?
A computer is an electronic device capable of performing computations, and we all
know that it is composed of a monitor, keyboard, mouse, and memory to store
information. But the most important component of the computer is a PROCESSOR.
Which does all thinking of computer, but the question is how the computer does this
thinking? How does it understand text, images, videos, etc.?
Suppose if you want to tell the computer to add two number (1+2) which is
represented by some binary numbers (10000011), how are you going to tell the
computer? Yes, we going to use assembly language to get our code executed.
We are going to give the command to a computer in this format as shown below.
Your code to add two numbers in this language would be in this order.
But how are we going to do this? Back in 1950's when computers were huge and
consumed a great deal of power, you would convert your assembly code into
corresponding machine code to 1 and 0's using mapping sheets. Later this code will
be punched into the machine cards and feed to the computer. The computer will
read these code and execute the program. These would be a long process then until
ASSEMBLER came to help.
But alone assembler is not involved in this whole process; it also requires the
compiler to compile the long code into a small chunk of codes. With advancement in
software development languages, this entire assembly code could shrink into just
one line print f 1+2 A with the help of software called COMPILER. It is used to
convert your c language code into assembly code, and the assembler converts it into
corresponding machine code, and this machine code will be transmitted to the
processor. The most common processor used in PC or Computers are Intel processor.
Though present-day compilers come bundled with assembler can directly convert
your higher language code into machine code.
Now, with a change in processor, the assembly instructions will also change. For
example the
And obviously, with a change in Operating System, the level and nature of O.S level
calls will also change.
Step 3) This code is not understood by any platform, but only a virtual platform
called the Java Virtual Machine.
Step 4) This Virtual Machine resides in the RAM of your operating system. When the
Virtual Machine is fed with this bytecode, it identifies the platform it is working on
and converts the bytecode into the native machine code.
In fact, while working on your PC or browsing the web whenever you see either of
these icons be assured the java virtual machine is loaded into your RAM. But what
makes java lucrative is that code once compiled can run not only on all PC platforms
but also mobiles or other electronic gadgets supporting java.
Bytecode is understandable to any JVM installed on any OS. In short, the java source
code can run on all operating systems.
JVM Architecture
Let's understand the Architecture of JVM. It contains classloader, memory area,
execution engine etc.
1) ClassLoader
The class loader is a subsystem used for loading class files. It performs three major
functions viz. Loading, Linking, and Initialization.
2) Method Area
JVM Method Area stores class structures like metadata, the constant runtime pool,
and the code for methods.
3) Heap
All the Objects, their related instance variables, and arrays are stored in the heap.
This memory is common and shared across multiple threads.
Java language Stacks store local variables, and it’s partial results. Each thread has its
own JVM stack, created simultaneously as the thread is created. A new frame is
created whenever a method is invoked, and it is deleted when method invocation
process is complete.
5) PC Registers
PC register store the address of the Java virtual machine instruction which is
currently executing. In Java, each thread has its separate PC register.
Native method stacks hold the instruction of native code depends on the native
library. It is written in another language instead of Java.
7) Execution Engine
The Native Method Interface is a programming framework. It allows Java code which
is running in a JVM to call by libraries and native applications.
Native Libraries is a collection of the Native Libraries(C, C++) which are needed by the
Execution Engine.
1) Editor – To type your program into, a notepad could be used for this
2) Compiler – To convert your high language program into native machine code
3) Linker – To combine different program files reference in your main program
together.
4) Loader – To load the files from your secondary storage device like Hard Disk, Flash
Drive, CD into RAM for execution. The loading is automatically done when you
execute your code.
With this background, refer the following video & learn the working and architecture
of the Java Virtual Machine.
Suppose in the main, you have called two function f1 and f2. The main function is
stored in file a1.c.
The next step is integrating all these object files into a single .exe file with the help of
linker. The linker will club all these files together and produces the .exe file.
During program run, a loader program will load a.exe into the RAM for the execution.
Java code Compilation and Execution in Java VM
Let's look at the process for JAVA. In your main, you have two methods f1 and f2.
The Java VM or Java Virtual Machine resides on the RAM. During execution, using the
class loader the class files are brought on the RAM. The BYTE code is verified for any
security breaches.
Next, the execution engine will convert the Bytecode into Native machine code. This
is just in time compiling. It is one of the main reason why Java is comparatively slow.
NOTE: JIT or Just-in-time compiler is the part of the Java Virtual Machine (JVM). It
interprets part of the Byte Code that has similar functionality at the same time.
The java compiler converts high-level java code into bytecode (which is also a type of
machine code).
In Java, the Just In Time Code generator converts the bytecode into the native
machine code which are at the same programming levels.
1. Dynamic Linking: Unlike C, linking is done at run-time, every time the program
is run in Java.
2. Run-time Interpreter: The conversion of byte code into native machine code
is done at run-time in Java which furthers slows down the speed
However, the latest version of Java has addressed the performance bottlenecks to a
great extent.
Summary:
• JVM or Java Virtual Machine is the engine that drives the Java Code. It
converts Java bytecode into machines language.
• In JVM, Java code is compiled to bytecode. This bytecode gets interpreted on
different machines
• JIT or Just-in-time compiler is the part of the Java Virtual Machine (JVM). It is
used to speed up the execution time
• In comparison to other compiler machines, Java may be slow in execution.