Sub topic 1
What the JDK Does
Java is a programming language: a way to write instructions for a computer. To create and run a Java program, you need tools that can understand those instructions.
The Java Development Kit, or JDK, is a collection of those tools. It includes a compiler, a way to start Java programs, and the parts needed to run them. A compiler is a tool that translates code into a form a computer can use.
You can think of the JDK as a toolkit for both building and running Java programs.
From Java code to a running program
A Java program starts as source code. Source code is text written by a person in a programming language. Java source code is usually stored in a file whose name ends in .java.
The JDK’s compiler is called javac. It reads Java source code and translates it into bytecode. Bytecode is a set of instructions made for the Java Virtual Machine, or JVM.
The JVM is a program that reads and runs Java bytecode. It works with the Java standard library, a collection of ready-made Java code for common tasks, such as displaying text.
The JDK also includes java, a tool that starts Java programs. It launches a JVM, which then runs the program’s bytecode.
A worked example
Imagine a Java program that displays the greeting Hello, World!. Here is what happens from start to finish:
- A person writes the program as source code in a file named
HelloWorld.java. - The
javaccompiler reads that file and creates bytecode. For this example, the bytecode is stored inHelloWorld.class. - The
javatool starts a JVM and asks it to run the program. - The JVM runs the bytecode. The program uses the Java library to display
Hello, World!.
The .java file and the .class file have different jobs. The .java file contains the instructions a person writes. The .class file contains the bytecode that the JVM runs. The compiler creates the second file from the first.
Compiling and running are separate steps. Compiling translates the code. Running carries out the program’s instructions.
The JDK, JVM, and runtime
These names are easy to mix up because the tools work together.
| Name | What it does |
|---|---|
| JDK | Provides tools to create and run Java programs. |
| `javac` | Compiles Java source code into bytecode. |
| JVM | Runs Java bytecode. |
| Java runtime | Provides what is needed to run Java programs, including a JVM and Java libraries. |
| `java` | Starts a JVM to run a Java program. |
You may also see the term JRE, short for Java Runtime Environment. A JRE is a runtime, not a full set of tools for creating Java programs. Some Java downloads do not offer a separate JRE. For this tutorial, the JDK provides both the compiler and a runtime.
Why the JVM matters
Different computers use different operating systems, such as Windows, macOS, and Linux. An operating system is the software that manages a computer and lets other programs run.
Java bytecode is designed to run on a JVM, rather than directly on one operating system. This means the same compiled Java program can often run on different systems, as long as each system has a compatible Java runtime.
That does not mean every program works everywhere without changes. For example, a program may depend on a file that exists on only one computer. Also, a runtime must support the version of the bytecode it is asked to run.
For now, remember the main idea: you write Java source code, the compiler makes bytecode, and the JVM runs it.
Check your understanding
Exercise
What is the difference between javac and the JVM?
Show answer
javac compiles Java source code into bytecode. The JVM runs that bytecode.
Exercise
In the greeting example, which file contains the source code, and which contains the compiled bytecode?
Show answer
HelloWorld.java contains the source code. HelloWorld.class contains the compiled bytecode.