Sample Java Program
Sample Java Program - Source Code
Our standard sample count-to-ten program in Java:
/*
Sample Java Program - Counts to Ten
Copyright 2006 Andrew Eichstaedt
Eichstaedt Development Group
http://www.andrew-eichstaedt.com
*/
class CountToTen {
public static void main(String args[]) {
System.out.println("Hello!");
System.out.println("I am a sample Java program");
System.out.println("that counts to ten.");
System.out.println("");
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
System.out.println("");
System.out.println("Thanks for running me.");
}
}
General Notes on Java
Java is an object-oriented language with a syntax similar to C++. A
major design objective of the Java language is cross-platform compatibility.
Unlike languages like C or C++, which are compiled directly into native
machine code, Java is typically compiled into into an intermediate byte
code format, which is run by a Java Virtual Machine (JVM) on the particular
architecture being used.
Java applications make use of the Java Runtime Environment (JRE). Installed
on the computer running the application, the JRE includes the JVM and
an extensive collection of class libraries.
Java applications may be run as command line programs, as in our example
program above. They also may be run as applets, embedded into a web page;
as servlets, running as processes on a server; or using a graphical user
interface implemented with the Swing or Abstract Window Toolkit (AWT)
libraries.
History of Java
Java began at Sun Microsystems in 1990 as part of a project to develop
smart appliances. It was intended as a smaller-footprint replacement for
C++, easily ported to a variety of devices. Another feature was the addition
of automatic garbage collection, which prevents the programmer from having
to explicitly handle memory management. By 1992, Java, originally named
Oak, had appeared.
In 1994, the language was rechristened Java and re-targeted towards Web
applications. The first public release of the Java Development Kit (JDK)
version 1.0, became available in January of 1996. Several revisions of
the language and the Java class library have been released since then,
with more planned in upcoming years.
Using the Example Java Program
First we compile the program, contained in our source file sample-java-program.java,
by invoking the Java compiler:
$ javac sample-java-program.java
This creates a bytecode class file named CountToTen.class. To run the
program, we launch the Java Virtual Machine:
$ java CountToTen
to produce the output:
Hello! I am a sample Java program
that counts to ten.
1
2
3
4
5
6
7
8
9
10
Thanks for running me.
Sample Java Program - Comments for Programmers
The first portion of the code within /* ...
*/ is a normal C-style comment. C++-style comments, begun by two
slashes at the end of the line, are also supported.
In Java, all working code exists within a class. In this case, we name
our new class CountToTen. The class
definition and other blocks of code are listed within curly braces ( {
and } ) as in C or C++.
Some explanation is required for the first line within the class definition:
public static void main(String args[])
This line defines a method named main,
which is special. The main method is
the starting point for the JVM to begin execution of the program. A more
complex problem than our sample will have many classes, but only one class
needs a main method. Note that when
writing Java applets, a main method is not used.
The public access specifier indicates
that the method will be visible to any class. Other access specifiers
are private and protected,
used to detote other levels of visibility.
The static declaration indicates that
main is a class method. This allows
it to be run without instantiating a particular instance of the CountToTen
class, which is necessary for it to be called by the JVM before any instances
of the class are created.
The void declaration, as in C, indicates
that no values is returned by the method.
The String args[] portion specifies
that the method takes an array of zero or more strings as command-line
arguments. In this case, we are using no command-line arguments, but the
parameter is required for the main method.
The next block of lines is of the form System.out.println("Hello!");
simply prints out the the strings specified in double-quotes.
The counterintive sytntax of what would be a print statement in many
langages exposes some of the complexity of Java's strict object model.
The System class is a collection of
useful objects and variables, including out,
which is an output stream directed at standard output.
The for loop uses fairly standard C/C++-style syntax:
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
It loops from one to ten, incrementing the variable i.
The first parameter to the for statement,
int i = 1, declares i
as an integer and sets the value of i
to one. The next parameter, i <= 10,
is a test expression returning a boolean value. If value is true, the
code in the block is run. The last parameter is also an expression, executed
with each iteration of the loop. Here, i++
increments i by one, just as in C or
C++.
The final block of println statements
simply print the goodbye message.
|