Java Reference
In-Depth Information
“Hello World.” Let us choose the program name to be
HelloWorld.java
. Then
we need to label the program class with that name too. To display a message,
we use the instruction
System.out.println(message);
. Instructions are followed by
a semi-colon “;” mark. Thus we get our slightly revised program:
Program 1.2
The Hello World Java program
class
HelloWorld
{
public static void
main ( String [
]
args )
System . out . println (
"Hello World"
);
}
}
Again, let us go through the workflow of editing/compiling and running the
program as follows:
-Type
this program and store it under filename
HelloWorld.java
.
- Compile
this program by typing in the console:
javac HelloWorld
(im-
plicitly meaning
javac HelloWorld.java
). A bytecode
HelloWorld.class
is produced in the same directory. The bytecode is not human readable, only
machine readable! Try to open the file and look at the strange sequence of
symbols that encode this compiled program. At this stage, it should be clear
that files ending with the extension
.java
are source codes, and files ending
with extensions
.class
are bytecodes.
-Execute
the program by typing the command
java HelloWorld
(implicitly
meaning
java HelloWorld.class
) at the console prompt.
You will now get the result visible in the output console as follows:
Hello World
Congratulations, you successfully wrote your first program. Let us now see how
to perform arithmetic calculations.
1.3 Expressions and programs as calculators
The first program you may think of is to compute formulas
4
for various input.
Assume, for example, we are given a 3D box with the following dimensions:
4
Historically, this objective was one of the very first motivations for programming
languages. FORTRAN, which stands for FORmula TRANslation, is such an
example that was widely used by physicists for simulation purposes. Although
FORTRAN is nowadays a bit outdated, there is still a lot of legacy code running
daily (for example, weather forecasting).
Search WWH ::
Custom Search