Java Reference
In-Depth Information
Program 1.1 My first Java program — A minimalist approach
class MyFirstProgram {
public static void main ( String [
]
args )
{}
}
At first, everything in the above code seems strange to novice programmers. In
fact, we will have to wait until Chapter 5 to get a full understanding of what
the first two lines actually mean. For now let us ignore these lines. To execute
this program, we need to perform the following three steps:
-Type this program into any text editor 1 and store this file under filename
MyFirstProgram.java . Extensions .java means that the file is a Java source
code . The name of the file should bear the name appearing after the class
keyword.
- Compile the program by typing at the console: javac MyFirstProgram.java .
This will generate a compiled file, called the bytecode , bearing the name
MyFirstProgram.class . Check in the directory 2 that this file was created.
-Execute the program by typing the command java MyFirstProgram at the
console prompt. This will execute the bytecode of MyFirstProgram.class .
That is, it will run the program on the java virtual machine 3 (JVM).
Well, the program ran correctly but produced no apparent result, so that it is
not very convincing to us that something really took place on the processor. In
fact, the main function of the program was called upon execution, and the set
of instructions contained inside the function was executed stepwise. Here, there
was nothing to execute since the body of the main function is empty; the body
of the main function is encapsulated into the program class MyFirstProgram
and is delimited by the opening/closing of braces {} , also called curly brackets.
So let us spice up this program by writing a message on the console output.
1.2.2 Hello World
The “Hello Word” program is the emblematic program for comparing at a
glance the syntax of different programming languages. What we need to do is
to add a single instruction line inside the former program to display the message
1 Like Notepad under Windows TM or Nedit in Linux-based KDE environments.
2 In Windows, type dir at the console prompt. In Unix, use the equivalent ls
command.
3 Java bytecode is cross-platform, which means that provided that there exists a JVM
implementation for the target machine, you will be able to run your bytecode on
that environment. This is one of the key strengths of Java in today's market.
 
 
Search WWH ::




Custom Search