ZetCode

Java language

last modified July 6, 2020

In this part of the Java tutorial, we will introduce the Java programming language.

Goal

The goal of this tutorial is to get you started with the Java programming language. The tutorial covers the core of the Java language. This tutorial uses command line compilers to build applications.

Java

Java is a high-level, general-purpose, object-oriented programming language. The main design goals of the language were robustness, portability, high performance and security. Java is a multithreaded and distributed programming language. It can be used to create console applications, GUI applications, web applications, both on PCs or embedded systems.

Java is a programming language created by Sun Microsystems in 1991. The first publicly available version of Java was released in 1995. Today, the language is developed by Oracle corporation.

Java excels in creating portable mobile applications, programming various appliances and in creating enterprise applications.

Java popularity

There are currently several widely used programming languages. Java belongs to the most popular languages today. Several surveys put it into the top three languages in the world.

Java platforms

Java has four programming platforms:

All Java platforms consist of a Java Virtual Machine (JVM) and an application programming interface (API). The Java Virtual Machine is a program, for a particular hardware and software platform that runs Java applications. An API is a collection of software components that we can use to create other software components or applications.

Java SE is used for developing desktop applications. Java SE's API provides the core functionality of the Java programming language. It consists of a virtual machine, development tools, deployment technologies, and other class libraries and toolkits used in Java applications. Java EE is built on top of the Java SE platform. The Java EE platform provides an API and runtime environment for developing and running web applications and large-scale, multi-tiered, scalable, reliable, and secure enterprise applications. Java ME is a subset of the Java SE. It provides an API and a small-footprint virtual machine for running Java applications on small devices, like mobile phones. JavaFX is a platform for creating rich Internet applications using a lightweight user-interface API.

Strictly speaking, Java SE is a platform specification. Java Platform, Standard Edition Development Kit is an official implementation of the Java SE by Oracle. There are also other implementations. For example free and open source OpenJDK or IBM's J9.

In our tutorial, we use the Java SE platform to create simple console applications.

JVM

Java virtual machine (JVM) executes Java bytecode. The JVM is included in the JRE and JDK. Java source code is written in files with the .java extension. The javac Java compiler will compile the Java source code into the Java bytecode; the compiled files have the .class extension. This bytecode is executed by JVM. The java tool is a launcher for Java applications. Oracle's JVM is called HotSpot. HotSpot is a Java virtual machine for desktops and servers. It has advanced techniques such as just-in-time compilation and adaptive optimization designed to improve performance.

JRE

JRE (Java Runtime Environment) is a set of tools for executing Java applications. The JRE does not contain tools and utilities such as compilers or debuggers for developing Java applications.

JDK

JDK (Java Development Kit) is a superset of the JRE. It contains JRE and tools such as the compilers and debuggers necessary for developing Java applications. We need to install JDK to build and run our Java programs.

OpenJDK installation

Due to Oracle's licencing issues, many developers turn to OpenJDK. Amazon provides no-cost, multiplatform, production-ready distribution of the Open Java Development Kit (OpenJDK); it is called Amazon Coretto.

$ tar xzvf openjdk-13_linux-x64_bin.tar.gz

For our studying purposes, we can use OpenJDK from https://jdk.java.net/.

$ ls -F jdk-13/
bin/  conf/  include/  jmods/  legal/  lib/  release

After we download and unpack OpenJDK, we can see the contents of the JDK in the jdk-13 directory. The development tools are located in the bin subdirectory. The javac compiler and the java application launcher are located in this subdirectory.

The conf directory contains the .properties, .policy, and other configuration files intended to be edited by developers, deployers, and end users. The include directory contains header files that support native-code programming. The jmods directory contains the compiled module definitions. The legal directory contains copyright and license files for each module.

The release file contains the JDK release information.

Setting environment variables

In the next step, we set the JAVA_HOME variable and update the PATH variable.

$ export JAVA_HOME=~/jdk-13/

The JAVA_HOME variable is used by tools such as IDEs or builders.

$ export PATH=$PATH:~/jdk-13/bin/

By updating the PATH variable, we do not need to specify the full path for the javac and java tools.

Compiling a Java application

We create a simple Java program using command line tools.

$ mkdir -p src/com/zetcode

Inside the current working directory, which is the main project directory, we create the com/zetcode subdirectory. Java source files are organized in modules called packages. The packages must match the directory structure.

$ mkdir bin

The compiled Java bytecode goes to the bin directory.

Note: In Java, the public class name must match the name of the file in which it is defined.
$ touch src/com/zetcode/SimpleEx.java

A SimpleEx.java source file is created in the com/zetcode subdirectory. Java source files have a .java extension.

com/zetcode/SimpleEx.java
package com.zetcode;

public class SimpleEx {

    public static void main(String[] args) {

        System.out.println("This is simple Java example.");
    }
}

This is a source code for a simple Java example. This example prints a message to the console.

package com.zetcode;

The package name must correspond to the directory structure in which the source file is located.

public class SimpleEx {

The public class name is required to match the file name.

$ javac -d bin src/com/zetcode/SimpleEx.java

Using the javac compiler, we compile the source code. Notice that we compile the Java source code from the root project directory. The compiled files go the bin directory.

$ tree
.
├── bin
│   └── com
│       └── zetcode
│           └── SimpleEx.class
└── src
    └── com
        └── zetcode
            └── SimpleEx.java
6 directories, 2 files

The compiler generates Java bytecode, which is executed by the Java Virtual Machine. The bytecode has a .class extension.

$ java -cp bin com.zetcode.SimpleEx
This is simple Java example.

With the java application launcher, we execute the program. It starts a Java runtime environment, loading a specified class, and invoking that class's main method. The .class extension is excluded; it is assumed. The program name is a fully qualified name of the program — com.zetcode.SimpleEx. It includes the name of the program and its package. With the -cp option we tell the launcher where to look for the class files.

Running single-file source code

Since Java 11, it is possible to run single .java files without defining a package structure and without the need to compile the source code first.

$ ls
SimpleEx.java

There is only one single file in the project directory.

SimpleEx.java
public class SimpleEx {

    public static void main(String[] args) {

        System.out.println("This is simple Java example.");
    }
}

We don't have to define a Java package.

$ java SimpleEx.java
This is simple Java example.

We run a simple application consisting of one file with the java tool. This is very convenient for learning purposes.

Sources

The following sources were used to create this tutorial:

In this part of the Java tutorial, we have introduced the Java language, provided some basic Java definitions, showed how to install the JDK, and created our first simple Java program.