Using plain Java

We outline the process of:

  1. Editing source code.

  2. Compile Java source code thereby generating so called class files.

  3. Executing class files by virtue of a piece of software being called the Java Virtual Machine.

    Main idea: Write once, run everywhere.

Figure 31. Edit - compile - execute Slide presentation

We now examine these three steps in greater detail starting with editing source code:

Figure 32. Editing Java files Slide presentation
Editing Java™ files

The process of creating a text file from scratch or editing an existing one is called editing. It requires a text editor like nano, vi(m), atom or similar.

Tip

You should learn quickly using a text editor of your choice. A basic command is sufficient. Both nano and vi(m) are good choices on Linux/UNIX systems due to their widespread distribution. Windows and Apple users have different choices as well.

The resulting files are just plain text. Unlike office documents they won't contain any layout or formatting like e.g. different font sizes, background colours or similar.


We shed some light on Java class definitions:

Figure 33. Defining class HelloWorld Slide presentation
// Filename HelloWorld.java 

public class HelloWorld  {

  public static void main(String[] args)  {
    System.out.println("Hello, world"); 
  }

}

The text file HelloWorld.java contains a class definition. Classes are Java's main building blocks. Real world Java applications consist of up to thousands of interacting classes.

... public class HelloWorld ... actually defines a so called Java class of name HelloWorld. We will refer to it as class HelloWorld for short. You'll learn about classes starting from Objects and Classes. The following restrictions apply:

  1. Class names must obey Java rules for so called identifiers. Spaces, operators like + and a lot of other characters are disallowed. More details are to be found in Figure 73, “Identifier name examples: ”.

  2. A Java file's base name must match the primary (the one being defined using the public modifier) class name it contains. In the given example the file's base name is HelloWorld. Defining e.g. ... public class Hello {... inside a file HelloWorld.java would be flagged as a compile time error.

public static void main(String[] args) is a so called class method definition.

For the time being we present a rule of thumb: Every Java program to become executable requires at least one class containing (nearly) exactly the following main(..) method definition:

public static void main(String[] args)

A statement example. This is our toy example's actual payload: Only this line of code has an observable effect on execution. All surrounding text is just boilerplate code being required to keep the Java environment happy.


Prior to execution we require a compilation step:

Figure 34. Compiling Java file Slide presentation
Compiling Java™ file Sourcecode Java compiler Executable bytecode file

The text file HelloWorld.java containing our HelloWorld class.

The Java compiler is a piece of software belonging to a JDK. It will read the input filename HelloWorld.java and create a resulting output filename HelloWorld.class.

A Java bytecode file contains instructions to be interpreted by a Java run time system (JRE), see next step. Bytecode files are not meant to be viewed or edited.

The main advantage of bytecode files is becoming independent of operating systems. Thus a given HelloWorld.class file may be executed on Linux, Windows, Mac-OS or any other JRE providing system in exactly the same way.


Figure 35. Command line Java file compilation Slide presentation
~/tmp$ ls -al HelloWorld.class 
ls: cannot access 'HelloWorld.class': No such file or directory

~/tmp$ javac HelloWorld.java 

~/tmp$ ls -al HelloWorld.class 
-rw-r--r-- 1 goik fb1prof 419 Sep 23 15:44 HelloWorld.class

Besides human readable portions bytecode files mainly contain binary data providing instructions to be executed:

Figure 36. Java byte code file HelloWorld.class Slide presentation
Êþº¾^@^@^@6^@^]
^@^F^@^O        ^@^P^@^Q^H^@^R
^@^S^@^T^G^@^U^G^@^V^A^@^F<init>^A^@^C()V^A^@^DCode^A^@^OLineNumberTable^A^@^Dmain^A^@^V(\
  [Ljava/lang/String;)V^A^@
SourceFile^A^@^OHelloWorld.java^L^@^G^@^H^G^@^W^L^@^X^@^Y^A^@^LHello, world^G^@^Z^L^@^[^@\
  ^\^A^@
HelloWorld^A^@^Pjava/lang/Object^A^@^Pjava/lang/System^A^@^Cout^A^@^ULjava/io/PrintStream;\
  ^A^@^Sjava/io/PrintStream^A^@^Gprintln^A^@^U(Ljava/lang/String;)V^@!^@^E^@^F^@^@^@^@^@^\
  B^@^A^@^G^@^H^@^A^@    ^@^@^@^]^@^A^@^A^@^@^@^E*·^@^A±^@^@^@^A^@
^@^@^@^F^@^A^@^@^@^B^@  ^@^K^@^L^@^A^@  ^@^@^@%^@^B^@^A^@^@^@   ²^@^B^R^C¶^@^D±^@^@^@^A^@
^@^@^@
^@^B^@^@^@^D^@^H^@^E^@^A^@^M^@^@^@^B^@^N

Figure 37. Source code vs. bytecode Slide presentation
HelloWorld.java HelloWorld.class
  • Human readable (kind of 😆).

  • High abstraction level.

  • Text file

  • Machine readable instructions.

  • Non-editable (usually).

  • Binary file.


Figure 38. Executing byte code file HelloWorld.class Slide presentation
Executing byte code file HelloWorld.class

Passing a bytecode file to the Java Runtime System (JRE):

java HelloWorld

Caution

The .class extension must be omitted! Trying java HelloWorld.class will yield an error.

The bytecode input will pass various steps inside the JRE. On successful processing the underlying operating system will eventually execute the desired actions on behalf of the JRE.

Terminal and keyboard are being linked to the operating system by two pre-connected communication channels called standard input and standard output respectively. In our toy example System.out represents the standard output channel and thus allows for execution of println("Hello, world").


Figure 39. Command line byte code file HelloWorld.class execution Slide presentation
> java HelloWorld 
Hello, world

Remark: This executes HelloWorld.class rather than HelloWorld.java.