4.INTRODUCTION TO JAVA BYTE CODE, JAVA VIRTUAL MACHINE

Before we start learning Java programming, we must know about the real force behind which is responsible for getting our hand-written Java program executed by computers CPU.

In previous chapter, we got introduced to JDK/JRE. You went to Oracle website & downloaded JDK or JRE. This is the first & foremost stop in order to run a Java program on your OS. If you don’t have this, your computer even does not even know what is Java. These are the software which have necessary compilers (included in them) to compile our Java file into machine language.

Computer says – you write program in C, C++, Java, Python, I don’t care. Just provide me the instruction in machine language. However, these IT software (JDK/JRE) include much more than just compiler.

  • JDK stands for Java Development Kit
  • JRE stands for Java Runtime Environment
  • JVM stands for Java Virtual Machine
  • JBC stands for Java Byte Code
  • JDK is superset enclosing JRE and JVM.

Following simple diagram gives a high-level view of —

java 4.1
  • We know that executing a Java program is two-step process
javsa 4.2

Step 1 is called development step. Why? Because this is the step in which we write (develop) Java program(s). Step 2 is execution step. Once we have .class files created we can execute it unlimited no. of times.

JDK as the name suggests & clear in above diagram is the superset software which includes two components:

  1. JRE
  2. Development tools like javac.
  • So, will we be able to perform step-1 without JDK?
    Answer is No because JDK only provides ‘javac’ for compiler, compiling the program written by user into .class.
  • So, will we be able to perform step-1 with only JRE installed on your OS but no JDK?
    Ans is NO. Because JRE does not provide development tools like javac.
  • So will we be able to perform step-2 with only JRE installed on your OS but no JDK?
    Ans is Yes. JRE has necessary tools included to execute .class files. If you have .class files you can run (execute) it as many times as you want.

Remember Trick:

  • Java Development Kit has the word development — means if you need to develop the program to execute the program.
  • Java Runtime Environment has the word run — means if we need to run (execute) the program.

JDK provides us all required tools needed to develop a Java program. It also contains other essential tools such as:

  • javac to compile .java file to .class file
  • java
  • jar
  • javadoc

We will get to know more details later.


DO IT YOURSELF

  1. Download & Install JRE on your OS. If you have already installed JDK, uninstall it and then install JRE. \
  2. First Install the IDE viz. eclipse. \
  3. Go to Preferences > General > Java > Installed JREs. Add.
  4. Add the path of your JRE installation here.
  5. Try to run a simple program from previous chapter both through CMD and eclipse. What do you see? Are you able to run the program? No why? You installed JRE & not JDK then ideally you should not have javac tool to perform Step-1 (compilation of .java into .class). But still you are able to do so. Why? Any guesses?

It is because, when you install IDE (not sure of you are), javac already comes that packaged with the IDE. Not sure if all IDEs support this but eclipse does. So even if you don’t have javac in your computer, you have it inbuilt in your eclipse.

Question: Check if you understood? Suppose, your school principal contacts you & asks you to create a program to help him in conducting school leader election through computer. He asks you to develop the program for the same & that program will be running on 5-6 computers on election day (so that it can be quickly finished).

You decide to develop that program on your personal computer & test it so that it works properly & then you will send .class file (or a group/collection of .class files) to your principal so that he can run on those 5-6 computers. What do you think should be installed on your computer? JDK or JRE

  • Those 5-6 computers? JDK or JRE

→ JRE JVM

By now, we understood a bit about JDK & JRE. They are the names of software which your OS must need in order to develop/run any Java program. Needless to say, software within software is also possible, as we see JRE is software which is present inside another software called JDK. However, it can exist as a separate standalone software on your OS.

The core of these two software (JDK/JRE) is JVM which stands for Java Virtual Machine.

ANALOGY 4.1: JVM can be thought of equivalent to CPU in computer hardware. As simple as that!

  • First of all clear basic fact — don’t go by the name ‘machine’ & think it is hardware. It is software. That’s why a developing company has put the name as Virtual Machine. I hope you understand the meaning of virtual.

Now, before understanding JVM, we need to understand JBC (Java Bytecode).

  • We saw little while ago that executing a Java program is two step process:
  • Step 1: Compiling .java into .class. javac tool does that.
  • Step 2: Executing .class file. java tool does that.

It is time to refine our understanding here, as the above two steps are over-simplified. You might have developed an understanding that .class contains machine language (i.e. 0s & 1s) which CPU can understand & execute. However, the fact is this: .class file contains Java bytecode. It is a special format file generated exclusively in Java programming language. Now in the next step this .class file will be compiled into machine language for CPU’s execution.


So, it is now like this —

java 4.3

In other programming language like C/C++ it is just one step:

java 4.4

That means compilers in those languages directly translate the program written in human understandable high-level language into machine language.

Why this 2-step process in Java? Why intermediate? You ask. The answer is, my dear reader, that JBC is the force due to which Java is called a platform-independent programming language.

What is the meaning of platform-independent programming language?

ANALOGY 4.2: Remember the case when you were required to write a program for voting in school election. You will soon understand why Java holds the user from behind the head!

Consider this scenario:

  • Your computer is having Windows OS running on a X-86 Intel Processor computer.
  1. 2 computers in your school are running on Mac (Apple) OS on ARM based Apple processor.
  2. 2 computers in your school are running Linux OS running on Pentium processor.
  3. 2 computers are running on Windows.

Case 1 / Scene 1: Suppose you don’t know Java, but C. You write the election program in text editor.

  • You save the file as Election.c
  • Your school principal does not need this file. He needs the file which he could execute on his 6 computers (He just double-clicks on the file you share with him & then his computers’ CPU starts executing the election program).
  • You invoke C compiler (gcc) to compile your program Election.c.
  • For this, you open your OS’s Command Prompt(cmd) & from the current directory, CMD points to, something else — you need to point to the directory where your file Election.c is kept.
  • Type gcc Election.c
  • This command will ask C compiler (gcc) to translate your Election.c (written in human understandable high-level language) into machine language code.
  • If there is no error in your program Election.c, you will be amazed to see Election.exe having created in the same folder.
  • Congratulations, C compiler has done his job. Election.exe is machine language code. It is also called executable.
  • Double click on the Election.exe. Windows OS is programmed to understand that .exe files can be executed by CPU & so OS hands over the instructions in .exe file to CPU for execution. CPU starts executing the instructions & accordingly output is displayed.
  • Once the election program runs properly as expected, you send this .exe file through a secondary storage device (say pendrive).

Scene 2: Principal takes the pendrive from you. Inserts the pendrive into each of his 6 computers & copies the files Election.exe into each computer. So far so good. Now he starts executing this executable on each computer one by one. This is what he will observe:

  • 1st computer of ③ — Runs perfect
  • 2nd computer of ① — Runs perfect
  • 3rd computer of ② — Does not run!

Platform means combination of OS & processor as we have given examples earlier.

  • 2nd computer of ② — Does not run
  • 3rd computer of ① — Does not run
  • 2nd computer of ② — Does not run

The principal is disappointed. Today is election day. Now the principal has to deploy only these 2 computers for conducting the election although he has 6 computers. It will take him 3 times more time to finish the election.

So what went wrong? The program you wrote is absolutely correct. That’s why it ran perfectly on a Windows computer. But it didn’t run on other platforms. This is because the Election.exe will run on the same/similar platform as it was generated on. Now it is again complex architecture involved here. But for now understand that C is a platform-dependent programming language. Hence, the output (.exe) generated for one platform will be specific to that platform. Any other platform will not be able to understand that .exe file. This is one of the drawbacks of C which is solved in Java.

Now, take reverse gear. You write the election program in Java.

Case 2 / Scene 1: You write the election program in Java.

  • You save the file as Election.java
  • Again, your school principal does not need this file. He needs the file which he could execute on his 6 computers.
  • You invoke javac (Java compiler) to compile your program i.e. Election.java.
  • For this, you open your OS’s CMD.
  • Type: javac Election.java
  • As we have learnt, this will generate Election.class in the same folder where Election.java exists.
  • You send this Election.class file to your school principal.

Case 2 / Scene 2: Principal does the same steps as in the previous case. But this time it will run on all his 6 computers perfectly. Needless to say, all 6 computers need to have JRE or JDK installed. Also bear in mind that since only executions are required on activity computers, having JRE installed is enough.

This is the biggest advantage Java has over other languages. We write code/program once & run on any platform. This is called WORA (Write Once Run Anywhere). Hence, Java is a platform-independent language. You write program on one platform & run it on a same platform + other platforms.

9.4 java

But in C:

4.6 java

This .class file is like some superman which runs on all platforms. This superman is Java Byte Code. It is a special format which is common for all platforms.

Refer Fig 4.3. That ? is nothing but the JVM. JVM is the software which translates the Java Byte Code (JBC) into machine language which will be different for different platforms.

You may ask if CPU understands just 0&1, then why machine language code is different for each platform. It is because each processor follows a specific Instruction Set Architecture (ISA). So machine code is generated as per ISA of the platform. More details on ISA are outside the scope of Java & can be read in Computer Organization / Computer Architecture course.

ANALOGY 4.3: Suppose there is a Russian company whose CEO knows only Russian language. This company operates in 4 countries speaking Spanish, French, German and Arabic. The CEO needs to pass certain instructions to his workers working in those 4 countries. The problem is the workers of those countries know only one language of their country respectively. He has 4 representatives from each country. Those 4 representatives know English as common language in addition to the language of their country.

The CEO writes the instructions to be passed in the language of his comfort i.e. in Russian. His PR translates this into English. Now she passes one copy of English text to all 4 representatives. Now those 4 representatives can translate English text into text of their respective language. CEO had to write once & his instructions are correctly created in 4 countries.

4.7 java

So completing Fig 4.3:

4.8

So main points about JVM is that it translates .class file into machine language code. The main point to note about JVM is that it is platform dependent.

It is easy to understand per analogy. Spanish representative can translate English into Spanish but not into any other language. Hence we need language-specific representatives.

That means JVM for platform 1 is different from platform 2 & so on. So, JVM1 ≠ JVM2 ≠ JVM3

That is, JVM is specific to your computer’s platform. That’s why you need to select the OS & processor bit when we download JDK or JRE (after all, JRE contains JVM).


DO IT YOURSELF

  1. By now you must have got basic understanding of JRE, JDK, JBC & JVM.
  2. Revise it, Read it, Re-read it & get the concept thoroughly before moving to next topic.
  3. We will discuss JVM in more detail in next section which will be very essential on the path of you becoming an excellent Java engineer.

MORE ABOUT JVM — JVM Advanced

So by now we understand that JVM is a blackbox where i/p is .class file and o/p is machine code, native to the platform of the computer.

4.9

Let’s open the box of JVM to see what is inside JVM. This is what JVM consists of… Looking at the names might sound you little difficult/technical, but gradually you will be comfortable with those names as you learn more about them.

4.10

Some of these terms will come more often during your programming journey. (Pareto principle applied — 20% of the terms will appear 80% times)

Recall from Fig 4.1, CPU has to work with data & instructions present in RAM (Primary memory). It does not work with data & instructions in secondary memory. Same fact applies here.

When CPU is executing instructions of JVM (i.e. running the JVM in other words) the data and instructions are brought in primary memory from secondary memory. While any software is executed, the data goes to/from secondary memory moves to/from primary memory millions of times.

So here also when JVM is being executed by CPU, CPU allocates certain section/part of the memory to JVM. This part of the primary memory is JVM memory as designated/labelled in the diagram. As JVM has been programmed, this CPU subdivides this part of primary memory into further sub-parts — method area, heap, JVM language stack etc. Now CPU does not know these names. This is for our use to refer.


Java Library

This library can be understood as similar to library in our real life. Have you ever visited a library? It is a large hall or rooms which contain books from various topics kept on shelf. Similarly, when you download JDK or JRE, you get a plethora of Java classes containing methods. We know classes & methods as we read from previous chapters. These methods cannot be directly called in the programs we write. This is of great advantage for programmers like us as we are saved from rewriting the code ourselves. For e.g. it has methods like sqrt() which can give us square root of a number.

These classes provided in JDK or JRE are high in numbers so they have been systematically divided into packages. If you open any .Java file, the first thing mentioned is package which tells which package does this class belong to. Package can be thought of like shelves in our real library. One shelf is dedicated to one subject. There can be further sub-divisions in a shelf like one rack. One shelf contains books of maths but within that shelf, one rack contains algebra books, another rack contains geometry, the other rack statistics & so on.

These are some package names present in Java library:

  1. java.applet — It contains classes which can be used to develop a standalone application having GUI. For e.g. MSPaint or…
  2. java.lang — Most common package. It is automatically present in any Java program we write. We can always use a class’s method from java.lang package but just providing the class’s package name in our program.
  3. java.util
  4. java.sql
    …& soon

Let us come back to JVM architecture. We know that executing the Java program is two step process:

4.11

You complete step-1. Your .class file is created. It is saved in your secondary memory. It will remain there even if you switch off & restart your computer.

You issue “java” command (step-2). Your .class is fed into the JVM. JVM takes up your .class file. The first & foremost component of JVM is ClassLoader. Loader receives this .class file.

ClassLoader has further three parts:

4.12

Loader —

  • Step 1: Reads the .class file
  • Step 2: Generates the binary data
  • Step 3: Saves the binary data into method area (Part no. ②③)
  • Step 4: Java library too (as we read) also has a class named as “Class”. Whenever the loader receives a .class file, it creates an object of “Class” type. For e.g. You wrote a program Car.java & compiled it. You get Car.class. As you will run Car.class, an object of “Class” type is created. This object is stored in heap memory (Part no. ③ in the diagram).

Understand it once more. You create Car class.

class Car
{
  ...
}

You know you can create as many objects of class Car type as you want like —

  • Car tataMano = new Car();
  • Car DavidCa = new Car();
  • Car mercedes = new Car();

Similarly, one object of class “Class” type is created in heap memory (③) as soon as loader gets the Car.class.

So loader creates the object of what type? Ans is Class type (of class).

4.13

Now you have Class objects, so you can see any behaviour of this object i.e. you can invoke methods of Class class. You might ask what are useful methods available in this Class type object. These contain methods like getting name of the class (i.e. Car), parent class name (which we will later read about parent/child), methods of Car class (behaviour), variables of Car class (attributes). Recall these concepts from previous chapters. Only one object is created per .class file.

It is interesting to know that Java library does not only contain a class with name “Class” but it also contain a class with name “Object”. It is present in java.lang package. It is parent class for all classes including the classes we write in our Java program.

Previous Article
Next Article