Java Development and Compilation

Java Development and Compilation

This article covers the different components that combine to produce a Java program.

Java Development Kit (JDK)

To develop Java programs, you need a development environment to work from. This is where the JDK comes in. JDK is a full-featured software development kit. When you download JDK, JRE (Java Runtime Environment) is also downloaded with it.

JDK also contains a number of development tools including the compiler (javac), JavaDoc, and Java Debugger.

To download the JDK, click this link: (Java Downloads | Oracle).

Java Runtime Environment (JRE)

JRE is a software package that provides Java class libraries, Java Virtual Machine (JVM), and other components that are required to run Java applications.

JRE is the superset of JVM. If you need to run Java programs, but not develop them, JRE is what you need. You can download JRE from Java SE Runtime Environment 8 Downloads page. Java source code is compiled and converted to Java bytecode (as shown below). If you want to run this bytecode on any platform, you need JRE. The JRE loads classes, checks memory access, and acquires system resources. JRE acts as a software layer on top of the operating system.

Java Virtual Machine (JVM)

JVM is an abstract machine that enables your computer to run a Java program.

When you run the Java program, the Java compiler first compiles your Java code to bytecode. Then, the JVM translates bytecode into native code.

Java is a platform-independent language. This is because when you write Java code, it's ultimately written for JVM but not your physical machine (computer). Since JVM ​executes the Java bytecode which is platform-independent, Java is platform-independent.

The JVM can also be referred to as the interpreter.

To demonstrate how Java compiles a program, let's look at a simple program:

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter your name: ");
    String name = input.nextLine();
    System.out.println("Hello " + name + "!");
  }
}

This program prompts the user to enter their name and then prints “Hello” followed by their name. It uses the Scanner class to read input from the user.

Don't worry about the different syntax and classes for now. We will cover them in more detail in later articles 🙂

To compile and run the program, you need to enter the following commands:

javac Main.java
java Main

Under the hood, the diagram below shows what's happening

JIT Compiler

From the Java source code (.java file extension), when you run javac Main.java, the compiler generates bytecode, which is then fed into the Java virtual machine (JVM) running on a system. The bytecode has a .class file extension as shown.

The JVM is part of the Java Runtime Environment (JRE). It interprets the bytecode and converts it to native code (or machine code) specific to the system processor architecture. The JVM also includes a JIT compiler for processing the bytecode, which yields better application performance.

What are bytecode and native code?

Bytecode is intermediate code generated by the compiler after source code compilation (at compile time). This intermediate code makes Java a platform-independent language. This is what makes Java portable.

Native code is binary code (1s and 0s) compiled to run on a specific processor. The code must conform to the processor's instruction set architecture (ISA). Native code provides instructions to the processor that describe what tasks to carry out. All instructions must be submitted to a processor as native code because it's the only language it can understand.

Each CPU includes an ISA that specifies what the processor can do. The ISA is built into the CPU as part of its microarchitecture and serves as an interface between the computer's hardware and software. The ISA defines the set of commands that can be performed by the CPU. The machine code must be compiled or assembled specifically for the processor's architecture and ISA.

At last! There you have it! 🎉 I hope you now have a good understanding of what happens when you run and compile your Java programs 👨‍💻. Feel free to reach out if you have any questions or comments!