Using plain Java™
We outline the process of:
-
Editing source code.
-
Compile Java™ source code thereby generating so called class files.
-
Executing class files by virtue of a piece of software being called the “Java Virtual Machine”.
Main idea: Write once, run everywhere.
We now examine these three steps in greater detail starting with editing source code:
❶ |
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. |
❷ |
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:
// Filename HelloWorld.java ❶
public class HelloWorld ❷ {
public static void main(String[] args) ❸ {
System.out.println("Hello, world"); ❹
}
}
The text file |
|
|
|
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
|
|
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:
❶ |
The text file |
❷ |
The Java™ compiler is a piece of software
belonging to a JDK™. It will read the input
filename |
❸ |
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
|
~/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:
Êþº¾^@^@^@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
HelloWorld.java |
HelloWorld.class |
---|---|
|
|
❶ |
Passing a bytecode file to the Java Runtime System (JRE™): java HelloWorld CautionThe |
❷ |
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 |
> java HelloWorld Hello, world
Remark: This executes HelloWorld.class
rather
than HelloWorld.java
.