“Hello, World” and friends.
No. 1
Extending class HelloWorld
Q: |
|
||||||||
A: |
After creating a file >javac HelloWorld.java ❶ > >ls -l ❷ -rw-r--r-- 1 goik fb1prof 426 Sep 25 09:14 HelloWorld.class ❸ -rw-r--r-- 1 goik fb1prof 124 Sep 23 17:44 HelloWorld.java ❹
Execution requires a Java™ runtime by virtue of the java command: There are several options for creating a whole paragraph spanning multiple text lines:
|
No. 2
Renaming a class file
Q: |
Consider HelloWorld.java again:
|
||||||||
A: |
|
No. 3
Editing and compilation
Q: |
Consider the following class definition in a HelloWord file:
On execution a newbie programmer is stunned still seeing
the former >java HelloWorld
Hello, world The file system view reads: >ls -l -rw-r--r-- 1 goik fb1prof 426 Sep 25 10:45 HelloWorld.class -rw-r--r-- 1 goik fb1prof 126 Sep 25 10:48 HelloWorld.java Whats wrong here? Why does the "I have changed!" string does not show up? |
A: |
The file system reveals
>ls -l -rw-r--r-- 1 goik fb1prof 426 Sep 25 10:45 HelloWorld.class -rw-r--r-- 1 goik fb1prof 126 Sep 25 10:48 HelloWorld.java The programmer likely edited
This is a common beginner's error: Integrated development environments like Intellij IDEA and build tools like Maven relieve you from the “manually keeping track of dependencies” burden: Editing a source code file will trigger compilation automatically. We'll turn to those higher level tools after finishing our basic tour on “pure” Java™ javac / java commands and plain text source code editing. |
No. 4
Editing bytecode
Q: |
We reconsider the bytecode example from Figure 34, “Java byte code file Êþº¾^@^@^@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: |
Overall conclusion: Tinkering with bytecode files is a bad idea unless you have a good reason and know what you are doing. |