Basic-1: Java Language: journey begins

Java has been designed since the very beginning as a robust strong typed language. The one of the main challenges and motivations was to solve a memory leaks issue by which C++ code was affected. The Java language has been intended for to control a small devices (eg. set-top box) which was a great idea those days but a future has shown the main direction. 

The code written in Java Language is translated into the byte-code. Such byte-code (a collection of the instructions) is executed by the Java Virtual Machine (JVM) in dedicated “sandbox”.

The JVM, the heart of the whole platform, is available for a different systems platforms.  In general it means that it’s possible to execute Java byte-code on different operation systems (Linux, Windows etc.). Such concept has been called WORA (Write Once Run Anywhere).

The advantages of Java (language/platform)  can be summarized as follows: Java has a great level of program encapsulation (eg. package or access modifiers). Java is a multithreaded language, which means it allows to process multiple operations in parallel wrapped by different threads that could be executed on different processors.  Java provides great level of security and robustness. The platform independency can be highlighted together with almost “backward compatibility” ( apostrophes refers to a new release cycle where internal APIs and libraries are changed, it means backward compatibility promise can not be hold each time, language evolution)

In this article we explore basic concepts of writing a Java code.

Java code that represents a program is written into a file. Such file ends by suffix.java”. Inside the file resides a Java class. For the simplification we consider trivial case, the file contains only one Java class .

In this simple example Java file has only one public Java class which similar name to the file.

The Java class may be instantiated by the Java program or other class. It means that an instance of such class will be created inside a JVM. JVM has “reserved” a physical memory where newly created object instance can reside. A Java program uses a “references” to all instantiated classes instances (objects) and JVM manages a memory together with a system resources. All those instances represent the “state” of the Java program. Each Java Program needs to have only one  entry-point which tells the JVM to execute it.

The entry-point is represented by the “main(String[] args)” method. The “main(String.. args)” method has follow specification:

  1. allows to pass the array of String arguments.
  2. is public and static, it means belongs to the Java class
  3. does not have to be instantiated

Let’s start for with a simple editor and create the 1st java file (Program)

File: SimpleProgram.java

public class SimpleProgram {
	public static void main(String[] args){
		System.out.println("Parameters size:" + args.length);
	}
} 

The file “SimpleProgram.java” contains a public class with the same name to the file. The class provides a static “main(…)” methods, which is executed as an entry-point by the platform.

Before we can execute the program we need to compile (translate) the “SimpleProgram.java” into the byte-code. Byte-code is the collection of the instructions that JVM understands and is able to execute them step-by-step (see bellow)

For such purpose we need to download a JDK (Java Development Kit) which contains the compiler. Nowadays there are multiple possible to choose from (OpenJDK, AdoptOpenJDK etc.). In this article  OpenJDK for selected platform is used. It is possible to verify whether java is present by simple command:

$java -version

output:
openjdk version "16" 2021-03-16
OpenJDK Runtime Environment (build 16+36-2231)
OpenJDK 64-Bit Server VM (build 16+36-2231, mixed mode, sharing) 

Having installed JDK it is possible to compile and execute a desired program. For the provided simple program there are two possible approaches:

# 1. classical approach with compiling and executing a java with compiled class
$ javac -g  -classpath out -sourcepath *src* -d *out* ./src/*.
$ java -cp out SimpleProgram

# 2. The release of JDK 11 comes with quite neat feature, “Launch Single-File Source-Code Programs” 
$ java ./src/SimpleProgram.java

# In both cases following output should be displayed:
# Parameters size:0
 

The byte-code view on “SimpleProgram” as a  collection of the instructions executed by the JVM:

$javap -c out/SimpleProgram.class

Compiled from "SimpleProgram.java"
public class SimpleProgram {
  public SimpleProgram();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: getstatic     #7                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: aload_0
       4: arraylength
       5: invokedynamic #13,  0             // InvokeDynamic #0:makeConcatWithConstants:(I)Ljava/lang/String;
      10: invokevirtual #17                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      13: return
} 

Conclusion

We have reviewed a basic block of the Java language and its platform, Class. We explored how to create a simple Java program and different approaches of executing it. 

References:

Main: Java tutorials