Fundamentals of Computing with Java: Java & JVM

Fundamentals of Computing with Java: Java & JVM

I recently started taking a refresher course on Java and I decided to share what I've learnt so far. A virtual machine (VM) is a software implementation of a computer that executes programs like a physical computer. Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode.

In this article, we are going to be looking at the fundamentals of computing with java, shedding light on the architecture of Java Virtual Machine(JVM) and code compilation and execution process in JVM.

The Java Platform architecture

Java development kit.png

Find below, the explanation of the architecture above:

sample.java → javac (sample. class) → JVM(sample.obj) → final output

First, java source code is converted into a .class file using the javac compiler. The class file code is in byte code form and that class file is used by JVM to convert into an object file. After that, you can see the final output on your screen.

The Java Development Kit (JDK) is a software development environment used to develop Java applications and applets. It contains JRE and several development tools, an interpreter/loader (java), a compiler (javac).

Why is Java Platform Independent?

A programming language is platform-independent if and only if it can run on all available operating systems with respect to its development and compilation. Now, Java is platform-independent because of the bytecode. Bytecodes are the machine language of the Java virtual machine. Bytecode execution in Java proves it is a platform-independent language.

What is the Java Virtual Machine (JVM)?

JVM stands for Java Virtual Machine. It is the engine that drives the Java Code. JVM is a Java platform component that provides an environment for executing Java programs. It converts Java bytecode into machine language.

  • In other programming languages, the compiler produces code for a particular system. But Java compiler produces code for a Virtual Machine.
  • In JVM, Java code is compiled into bytecode. This bytecode gets interpreted on different machines.
  • Between the host system and Java source, bytecode is an intermediary language.
  • JVM is responsible for allocating a memory space.

So, in summary, JVM performs the following functions:

  • Loads the code
  • Verifies the code
  • Executes the code
  • Provides runtime environment

Code compilation and execution in Java VM

To write and execute a software program, you need the following

  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 references 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.
  5. Execution – Actual execution of the code which is handled by your OS & processor.

Let's look a look at the compiling process in Java. Suppose you have two methods f1 and f2 in addition to main.

  • the main method is stored in file main.java
  • f1 is stored in the file f1.java
  • f2 is stored in the file f2.java

The compiler will compile the three files and produce a corresponding .class file which consists of BYTE code.

The Java VM or Java Virtual Machine which resides on the RAM, during execution, loads the class files into RAM using classloaders. The BYTE code is verified for any security breaches.

Next, the execution engine (JIT) will convert the bytecode into native machine code. This is just-in-time compiling.

JIT or Just-in-time compiler is the part of the Java Virtual Machine that interprets part of the bytecode that has similar functionality at the same time.

Why is Java both an interpreted and compiled language?

Programming languages are classified as:

  • Higher Level Language Ex. C++ , Java
  • Middle-Level Languages Ex. C
  • Low-Level Language Ex Assembly
  • finally the lowest level as the Machine Language.

A compiler is a program which converts a program from one level of language to another. Example conversion of C++ program into machine code. The java compiler convert's high-level java code into bytecode (which is also a type of machine code).

An interpreter is a program which converts a program at one level to another programming language at the same level. Example conversion of Java program into C++. In Java, the Just In Time Code generator converts the bytecode into the native machine code which are at the same programming levels.

Further Reading

  1. en.wikipedia.org/wiki/Java_virtual_machine
  2. edureka.co/blog/java-architecture
  3. https://dzone.com/articles/a-detailed-breakdown-of-the-jvm

Thanks for reading. I hope this article has increased your understanding of computing in Java. If you enjoyed reading, please like this article and follow me on Twitter.

Cheers 🙌

Did you find this article valuable?

Support Zainab Daodu by becoming a sponsor. Any amount is appreciated!