Video lecture
Watch on YouTube(opens in a new tab)Sub topic 1
What the JDK Does
The Java Development Kit, or JDK, is a set of tools for creating and running Java programs. It includes a compiler, which prepares your Java code to run, and a Java runtime, which runs the prepared code.
This chapter explains what those parts do and how they fit together. You do not need to install anything or run commands yet.
From an idea to a running program
A program is a set of instructions for a computer. You write Java programs using the Java programming language, which has rules for how to express those instructions.
The text you write is called source code. Java source code is stored in files whose names end in .java. A file named HelloWorld.java, for example, can hold the source code for a greeting program.
A computer does not usually run that source file directly. With the JDK, the usual path has two main steps:
- A compiler translates the source code into another form.
- A runtime uses the translated code to carry out the program.
A compiler is a tool that translates code and checks it against the rules of a programming language. The Java compiler in the JDK is called javac.
The Java runtime is the set of components needed to run a Java program. It includes a Java Virtual Machine and supporting libraries. You can start it with the java tool.
The compiler and runtime have different jobs. The compiler prepares the program. The runtime runs the prepared program.
What is inside the JDK?
The JDK brings together the main pieces you need as a Java developer.
| Part | Its job |
|---|---|
| Java compiler, `javac` | Checks and translates Java source code. |
| Java runtime | Provides the parts needed to run compiled Java programs. |
| Java launcher, `java` | Starts a Java program using the runtime. |
| Java standard library | Provides ready-made code for common tasks. |
| Other development tools | Help with tasks such as creating documentation and packaging programs. |
A library is a collection of ready-made code that programs can use. The Java standard library includes code for tasks such as displaying text, working with numbers, and reading files. Using a library can save you from writing every small operation yourself.
The JDK is more than a compiler. It gives you tools to develop a program and the runtime components to run it. Later chapters will guide you through getting the JDK and using its tools.
Source code, bytecode, and class files
When the Java compiler translates source code, it usually produces bytecode. Bytecode is a form of instructions designed for the Java Virtual Machine. It is not the same as the text you wrote, and it is not usually the direct instructions used by your computer’s processor.
The compiler stores bytecode in files ending in .class. These are called class files. A class is a way Java organizes code; you will learn more about writing one in a later chapter.
For a small program, the path might look like this:
| File | What it contains | Who makes it |
|---|---|---|
| `HelloWorld.java` | Java source code | The programmer |
| `HelloWorld.class` | Bytecode for a class | The Java compiler |
These files have different purposes. You edit the source file when you want to change the program. The class file is an output of compilation. One source file can produce more than one class file, depending on what it contains.
The class file is not a plain-text copy of the source file. It contains information in a form meant for the Java runtime, not for you to edit as source code.
What the Java Virtual Machine does
The Java Virtual Machine, or JVM, is the part of the runtime that loads and runs Java bytecode. It is called a virtual machine because it provides a working environment for programs rather than being a physical computer.
When a Java program starts, the JVM loads the class files it needs. It can check the bytecode to see whether it follows certain rules. Those checks are useful, but they do not prove that the program is safe in every situation or that it will give the answer you intended.
The JVM can run bytecode by interpreting it. Interpreting means reading instructions and carrying them out as the program runs. The JVM can also translate frequently used bytecode into instructions for the computer’s processor while the program runs. This is called just-in-time compilation.
You do not need to manage these details to write your first program. The key point is that the JVM bridges the gap between Java bytecode and the computer running it.
Why bytecode is useful
Different computers can use different operating systems and processors. An operating system is the main software that manages a computer, such as Windows, macOS, or Linux. A processor is the part of the computer that carries out instructions.
Java bytecode is designed to run on a suitable JVM rather than being written for just one kind of processor. This is the idea behind the phrase “write once, run anywhere.”
In practice, “anywhere” has limits:
- The other computer needs a suitable Java runtime for its operating system and processor.
- The runtime must support the bytecode and Java features the program needs.
- A program can still depend on things that differ between computers, such as files or other software it expects to find.
So Java makes it possible to use the same compiled program in many places, but it does not promise that every program will work unchanged in every situation.
JDK, JRE, and JVM
These three short names are easy to mix up. They refer to related things, but they are not interchangeable.
| Name | Full name | Main purpose |
|---|---|---|
| JDK | Java Development Kit | Provides tools to develop Java programs, along with runtime components. |
| JRE | Java Runtime Environment | Refers to the components needed to run Java programs. |
| JVM | Java Virtual Machine | Loads and runs Java bytecode as part of the runtime. |
Think of the relationship this way: the JVM is part of the runtime, and the JDK provides development tools as well as runtime components. The runtime also needs supporting pieces, including Java libraries; the JVM alone is not the whole runtime.
You may see older instructions that ask you to download a separate JRE. Some modern Java providers do not offer a separate JRE download. For learning to write Java, the important point is that you need a JDK: a runtime alone does not provide the Java compiler.
A worked example: a greeting
Imagine a complete Java program stored in HelloWorld.java. Its purpose is to display the greeting Hello, World!.
One line in the source code could be:
System.out.println("Hello, World!");
This line is a preview, not a complete program by itself. In a complete program, it belongs inside a structure that tells Java where the program starts. You will write that structure in a later chapter.
For now, notice the roles of the parts:
"Hello, World!"is the text to display.System.outidentifies a place for the program’s normal text output.printlntells Java to print the text and then move to a new line.
The line uses ready-made code from the Java standard library. Here is what happens to the complete program:
- Write: A programmer puts Java source code in
HelloWorld.java. - Compile: The
javaccompiler checks the source code and translates it into bytecode. - Store: For this small example, the compiler creates
HelloWorld.classto hold the bytecode. - Run: The
javalauncher starts the runtime, and the JVM loads the class. - Display: The program uses the standard library to produce the output
Hello, World!.
The source file does not become the output. It describes what the program should do. The class file contains the translated instructions, and the running program produces the greeting.
What can go wrong at each stage?
Knowing which part is working can help you understand an error message. You do not need to fix these problems yet; later chapters cover common problems in more detail.
During compilation
A compilation error is a problem the compiler reports when it cannot produce the needed class file from the source code. For example, Java has rules about how code must be written. If code breaks one of those rules, the compiler can report the problem.
A compiler can also catch some mismatches in the kind of data an instruction expects. For example, a piece of code may require a number but receive text instead.
If compilation fails, there may be no new class file to run. Fixing the source code and compiling it again is the usual next step.
While the program runs
A runtime error is a problem that occurs while a program is running. For example, a program might ask to read a file that does not exist. The compiler may not be able to know that the file will be missing when the program runs.
A program can also run without reporting an error and still give the wrong result. For example, it might display the wrong greeting because the programmer wrote the wrong words. The compiler checks many rules, but it cannot know every goal a programmer had in mind.
Changing a program
Suppose you change the greeting in HelloWorld.java. You have changed the source code, but an existing class file still contains the bytecode made from the earlier version.
For the usual compile-and-run workflow, compile the changed source again to produce updated bytecode. Then run the updated program.
If you have not changed the source or its needed components, you can usually run the compiled program again without compiling it again. Compilation and running are separate actions.
Keep the roles straight
When you meet a new Java term, connect it to a step in the process:
- Source code is the Java text you write.
javacchecks and translates that text.- Bytecode is the translated form stored in class files.
- The JVM loads and runs the bytecode.
- The runtime supplies the JVM and other pieces needed to run the program.
- The JDK supplies the development tools and runtime components.
You will use this model throughout the tutorial. In the next steps, you will prepare a place to work and get the JDK onto your computer. Later, you will write, compile, and run the greeting program yourself.
Exercise
You have a Java source file. Which JDK tool translates it, and what kind of code does that tool produce?
Show answer
The javac compiler translates the source file into bytecode, usually stored in one or more class files.
Exercise
A program compiles successfully, but it tries to read a file that is not there. Is that a compilation problem or a runtime problem?
Show answer
It is a runtime problem because it occurs when the program runs. Successful compilation does not guarantee that the file will exist.
Exercise
You change the greeting in a source file but still have a class file from an earlier compilation. Why compile again?
Show answer
The existing class file contains bytecode made from the earlier source. Compiling again produces bytecode that includes the change.