Skip to main content

Getting Started

This section will provide an overview of what Java is and how we go from Java code to a running computer program. To do this, we'll cover what the Java Development Kit is, how to install it, and finally using the Java Development Kit to run Java programs.

This page only provides a very brief overview intended to get you started with Java; the concepts discussed here will be covered fully in later pages.

Just what is Java?​

When programming in Java, we often write one or more Java source files (files that contain Java code). These contain the human-readable instructions that describe the operations our computer will perform when the Java code is run.

a file named 'MyFile.java'

In order to transform these human-readable source files into something that can be executed by a computer, they must first be compiled by a computer program called a Java compiler which converts them into Java bytecode. Java bytecode is stored in files called class files.

MyFile.java being passed into the Java Compiler and a file called 'MyFile.class' coming out the other side

Java bytecode contains simple operations that can be executed by a computer program called the Java Virtual Machine, often referred to by its initialism: JVM.

The MyFile.class file going into a terminal with the command 'java MyFile' being run with 'Hello, World!' being printed

The JVM runs our compiled Java program, and we can see the result of executing the operations we had originally written in the Java source file.

These tools, the Java compiler and the Java Virtual Machine, form part of what is called the Java Development Kit—commonly referred to by its initialism: JDK. The JDK is a collection of computer programs that allow us to work with, compile, package, document, debug, and run Java code.

So, when we refer to "Programming in Java", we are generally referring to writing and working with Java source files and using the computer programs from the Java Development Kit to compile, run, and process them. This section will briefly describe Java source files, the Java compiler, and the Java Virtual Machine, as these are the tools from the Java Development Kit that you will likely be using most often.

Java Source Files​

Java source files are text files that have the .java extension and contain Java code. Much like how the data on your computer is organized into folders and files, Java code must be organized into classes and methods.

Below is an example of a Java program that prints "1 + 1 = 2" when compiled and run. Line numbers, which are not part of the Java document, are shown next to each line so they can be referred to later.

AddExample.java
class AddExample {
static void main() {
var total = add(1, 1);
IO.println("One plus one is: " + total);
}

static int add(int x, int y) {
return x + y;
}
}

A few things to note about this Java file:

  • The whole file contains one class called AddExample
  • The contents of the AddExample class are wrapped in the curly braces spanning lines 1 to 10
  • The AddExample class contains two methods called main and add
  • The contents of each method are also wrapped in curly braces

The main method is a special method as it is where Java will start executing our program when we compile and run it; calling a method main is the way we indicate to the JVM where we want our Java program to start running. The main method contains two statements—instructions that will be executed by the JVM:

  • The first, on line 3, creates a variable called total and sets its value to the result the add method computes.
  • The second statement, on line 4, prints text to the user and uses the value of the total variable to compute what text should be displayed.

The add method contains one statement on line 8. All this does is add together two values and make the result of the computation available in the places where the add method is executed.

Note that every statement ends with a semicolon: ;. This is a requirement for Java statements.

Curly braces all the way down

You will notice that Java organizes a lot of things using curly braces: {...}

Pay special attention to them as you write Java code—mismatching curly braces, or adding one too many or too few will likely cause your program to crash or produce incorrect output.

Java Compilers​

The main task of a Java compiler is to process Java source files and produce one or more class files containing Java bytecode. Java bytecode contains instructions that the Java Virtual Machine can execute to run our Java program. Java compilers do much more than just produce bytecode, however.

Before producing bytecode, the Java compiler will check that our program doesn't contain various types of compile-time errors. If our source code contains these errors, the Java compiler will stop compiling our program, and print an error message with information about what error occurred and where in our Java source files the error occurred.

Some common errors that the Java compiler checks for are:

  • Forgetting to end a statement with a semicolon
  • Saying that a variable stores one kind of data, for example, a number, when it actually stores something else, such as text
  • Saying that a method returns a value when in some cases it might not
  • Having mismatched curly braces
  • And many more…

While it may be frustrating to get compiler error messages, they usually indicate a serious error in our program that should be fixed. By dealing with as many errors as we can at compile time, we generally reduce the likelihood of an error occurring when someone (for example, a client or customer) is using our program.

Java Virtual Machines​

A Java Virtual Machine (JVM) is a computer program that can run compiled Java programs.

Recall from the Java Source Files section that Java programs contain a special method called main. When we run our compiled Java programs with the JVM, we pass it the name of the class that contains this main method, and the JVM will start running the code that is inside the main method.

Who Decides How All of this Works?​

Java Standard Edition (Java SE) is a technical standard developed by Oracle (a multinational software company) that defines the Java language and how Java Compilers and Java Virtual Machines are supposed to behave.

Installing the Java Development Kit​

As mentioned previously, the Java Development Kit (JDK) contains several computer programs that allow developers to write, compile, and run Java programs. Among the programs the JDK provides are a Java compiler and a Java Virtual Machine.

This section will walk through how to install the Java Development Kit onto your computer.

warning

Make sure you install the Java Development Kit from a trusted source. When you install software, you are running someone else's code on your computer.

Where Does the Java Development Kit Come From?​

The Java Development Kit has several different implementations that are maintained by several different vendors. The following are a few different distributions of the Java Development Kit:

Oracle JDK
Oracle OpenJDK
An open-source and community-maintained JDK implementation managed by Oracle. Oracle OpenJDK serves as the reference implementation of Java SE; that is, Oracle OpenJDK serves as an example of a "correct" implementation of the Java SE standard.
Temurin
Microsoft Build of OpenJDK
Corretto
GraalVM
JetBrains Runtime

Why might you want to choose one JDK distribution over another?​

Licensing​

One of the main concerns around choosing a JDK distribution is around licensing: each JDK distribution has an associated licence that defines terms you must agree to in order to be able to use that distribution.

For example releases of Oracle JDK are initially licensed under the terms of the Oracle No-Fee Terms and Conditions Licence; which allows free use of the JDK for personal and commercial use. However, after some time, updates to Oracle JDK releases are licensed under the terms of the OTN Licence Agreement for Java SE; which permits free personal use, but requires a paid Java SE Universal Subscription for commercial use.

Oracle OpenJDK, on the other hand, is licensed under the terms of the GNU General Public Licence v2, with the Classpath Exception (GPLv2+CPE). This licence, among other things, allows you to use Oracle OpenJDK to develop your own Java programs for commercial and non-commercial purposes.

Support​

Another reason you might choose one JDK distribution over another is the level and duration of support provided. Some JDK distributions provide better, longer support than other distributions.

When a new version of Java SE is released, the distributor will indicate how long they intend to support that version; that is, how long they will continue to fix security issues and bugs. When support for a version of Java runs out, it is a good idea to upgrade to a newer and supported version.

Distributors will also indicate the level of support they will provide. For example, some distributors will provide contact details for support workers who will help diagnose and fix issues you may be having while using their JDK; whereas other distributors will provide the JDK as-is and not offer individual support.

For example, Oracle JDK typically provides longer support than Oracle OpenJDK. And, with a paid subscription, Oracle will provide individual support services to resolve issues using Oracle JDK; this level of support is not available using Oracle OpenJDK.

Features​

Some JDK distributors will add additional features.

What Version Should I Install?​

In addition to there being several different distributions of the JDK, Java SE also comes out with a new version around twice a year. Each new version of Java SE typically implements new features and performance improvements.

note

The Centre for Learning Java aims to remain up-to-date with a currently supported long-term support (LTS) release of Java.

Installation Instructions​

Linux​

MacOS​

Windows​

Running Our First Program​