Java language

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.

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. Basically, we have two options: Oracle JDK or OpenJDK.

JDK installation

On Linux distributions, we can install OpenJDK with a package manager.

$ sudo apt-get install default-jdk

This command installs OpenJDK on a Debian-based Linux.

$ tar -xzvf jdk-8u111-linux-x64.tar.gz

We download the Oracle JDK and decompress the file. Oracle's official JDK can be downloaded from this dowload page.

$ ls jdk1.8.0_111/
bin        javafx-src.zip  man          THIRDPARTYLICENSEREADME-JAVAFX.txt
COPYRIGHT  jre             README.html  THIRDPARTYLICENSEREADME.txt
db         lib             release
include    LICENSE         src.zip

After we download and unpack the Oracles's JDK, we can see the contents of the JDK in the our jdk1.8.0_111 directory. The development tools are located in the bin subdirectory. The Java javac compiler and the java application launcher are located in this subdirectory. The jre subdirectory contains the JVM, class libraries and other files that help execute Java programs. The lib subdirectory has some additional class libraries and support files. The db subdirectory contains the Java DB, which is the Oracle's distribution of the Apache Derby database. In the include subdirectory we can find header files that support native-code programming. The src.zip file contains source files for all classes that make up the Java core API.

Compiling a Java application

We create a simple Java program using command line tools.

$ mkdir -p 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.

$ touch com/zetcode/SimpleEx.java

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

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 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.

$ ls com/zetcode/
SimpleEx.class  SimpleEx.java

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

$ java 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.

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.