To write and run Java programs, we need tools to compile them and a machine to execute them. Java provides both.

JDK, JRE and JVM

The Java Development Kit (JDK) contains the tools for developing programs: javac (the compiler), java (the interpreter or launcher), javadoc (documentation generator), jdb (debugger), jar (archiver) and javap (disassembler). The Java Runtime Environment (JRE) contains what is needed to run programs: the JVM and the standard class libraries. The Java Virtual Machine (JVM) is the program that executes bytecode. The JDK includes the JRE, and the JRE includes the JVM.

Definition: Java Virtual Machine. An abstract computing machine, implemented in software for each platform, that loads, verifies and executes Java bytecode.

Bytecode and the JVM

The Java compiler does not produce machine code for a particular processor. It produces bytecode, the instruction set of the JVM, stored in .class files. Each platform has its own JVM, which translates bytecode into the machine code of that platform. Inside the JVM, the class loader loads classes, the bytecode verifier checks that the code is safe, and the execution engine runs it, using an interpreter and a JIT compiler that converts frequently run code to native machine code. Memory areas include the heap (objects), the stack (method calls) and the method area (class data). The garbage collector frees objects that are no longer used.

Implementing a Java Program

The steps are: (1) write the source code in a text file with a .java extension, named after the public class, such as Hello.java. (2) Compile it with javac Hello.java, which produces Hello.class. (3) Run it with java Hello, which starts the JVM, loads the class and calls its main method. From Java 11, a single-file program can also be run directly with java Hello.java. Programs can also be written in an IDE such as Eclipse, IntelliJ IDEA or NetBeans, which compile and run with one click.

Java API

Java comes with a large Application Programming Interface (API), a library of classes grouped into packages, such as java.lang (basic classes, imported automatically), java.util (collections, Scanner), java.io (input and output), java.net (networking) and java.awt and javax.swing (graphics).

Table 1.3: JDK tools

Tool Purpose Example
javac Compile source to bytecode javac Hello.java
java Run a class on the JVM java Hello
javadoc Make HTML documentation javadoc Hello.java
jdb Debug a program jdb Hello
jar Package classes into an archive jar cf app.jar *.class

Example 1.3: From source to output

Problem: List what happens when a programmer types javac Hello.java and then java Hello.

Solution:

  1. javac checks the syntax and produces the bytecode file Hello.class.
  2. java starts the JVM.
  3. The class loader loads Hello.class, and the verifier checks it.
  4. The execution engine calls main() and runs the bytecode, interpreting or JIT-compiling it.

Result: The program's output appears on the screen, on any platform with a JVM.

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
        System.out.println("Bytecode run by the JVM.");
    }
}

Output

Hello, Java!
Bytecode run by the JVM.

Common mistakes

  • Naming the file differently from the public class, which causes a compile error.
  • Typing java Hello.class instead of java Hello.
  • Confusing the JDK, which is for development, with the JRE, which only runs programs.

Key takeaways

  • JDK = development tools + JRE; JRE = JVM + libraries.
  • javac produces bytecode (.class); java runs it on the JVM.
  • The JVM has a class loader, a bytecode verifier and an execution engine with a JIT compiler.
  • java.lang is imported automatically.

Practice questions

  1. What is bytecode?
  2. What is the role of the JIT compiler?
  3. Distinguish JDK and JRE.

Answers to the practice questions

  1. The platform-independent instruction set produced by the Java compiler and executed by the JVM.
  2. It converts frequently used bytecode into native machine code at run time for speed.
  3. The JDK has development tools and the JRE; the JRE has only what is needed to run programs.

Sources

  • Official BCA syllabus Object Oriented Programming using Java, course code 25BCA402SEC01, Unit I