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 30. Edit - compile - execute Slide presentation

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

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


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 32. Defining class HelloWorld Slide presentation
// Filename 

public class HelloWorld  {

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


The text file 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 72, “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 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 33. Compiling Java file Slide presentation
Compiling Java™ file Sourcecode Java compiler Executable bytecode file

The text file containing our HelloWorld class.

The Java compiler is a piece of software belonging to a JDK. It will read the input filename 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 34. Command line Java file compilation Slide presentation
~/tmp$ ls -al HelloWorld.class 
ls: cannot access 'HelloWorld.class': No such file or directory

~/tmp$ javac 

~/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 35. Java byte code file HelloWorld.class Slide presentation
^@^F^@^O        ^@^P^@^Q^H^@^R
SourceFile^A^@^^L^@^G^@^H^G^@^W^L^@^X^@^Y^A^@^LHello, world^G^@^Z^L^@^[^@\
  B^@^A^@^G^@^H^@^A^@    ^@^@^@^]^@^A^@^A^@^@^@^E*·^@^A±^@^@^@^A^@
^@^@^@^F^@^A^@^@^@^B^@  ^@^K^@^L^@^A^@  ^@^@^@%^@^B^@^A^@^@^@   ²^@^B^R^C¶^@^D±^@^@^@^A^@

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

  • High abstraction level.

  • Text file

  • Machine readable instructions.

  • Non-editable (usually).

  • Binary file.

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


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 38. Command line byte code file HelloWorld.class execution Slide presentation
> java HelloWorld 
Hello, world

Remark: This executes HelloWorld.class rather than

Figure 39. JDK installation options Slide presentation