Thursday, July 19, 2012

A First Simple Java Program



Now that the basic object-oriented underpinning of Java has been discussed, let’s
look at some actual Java programs. Let’s start by compiling and running the short
sample program shown here. As you will see, this involves a little more work than
you might imagine.

/*
This is a simple Java program.
Call this file "Example.java".
*/

class Example {
// Your program begins with a call to main().
public static void main(String args[]) {
System.out.println("This is a simple Java program.");
}
}
Entering the Program

 For most computer languages, the name of the file that holds the source code to a program is arbitrary. However, this is not the case with Java. The first thing that you must learn about Java is that the name you give to a source file is very important. For this example, the name of the source file should be Example.java. Let’s see why. In Java, a source file is officially called a compilation unit. It is a text file that contains one or more class definitions. The Java compiler requires that a source file use the .java filename extension. Notice that the file extension is four characters long. As you might guess, your operating system must be capable of supporting long filenames. This means
that DOS and Windows 3.1 are not capable of supporting Java. However, Windows
95/98 and Windows NT/2000/XP work just fine.
As you can see by looking at the program, the name of the class defined by the
program is also Example. This is not a coincidence. In Java, all code must reside inside
a class. By convention, the name of that class should match the name of the file that
holds the program. You should also make sure that the capitalization of the filename
matches the class name. The reason for this is that Java is case-sensitive. At this point,
the convention that filenames correspond to class names may seem arbitrary. However,
this convention makes it easier to maintain and organize your programs.

Compiling the Program
To compile the Example program, execute the compiler, javac, specifying the name of
the source file on the command line, as shown here:

C:\>javac Example.java

The javac compiler creates a file called Example.class that contains the bytecode version
of the program. As discussed earlier, the Java bytecode is the intermediate representation
of your program that contains instructions the Java interpreter will execute. Thus, the
output of javac is not code that can be directly executed.
To actually run the program, you must use the Java interpreter, called java. To do
so, pass the class name Example as a command-line argument, as shown here:

C:\>java Example

When the program is run, the following output is displayed:

This is a simple Java program.

When Java source code is compiled, each individual class is put into its own output
file named after the class and using the .class extension. This is why it is a good idea to
give your Java source files the same name as the class they contain—the name of the
source file will match the name of the .class file. When you execute the Java interpreter
as just shown, you are actually specifying the name of the class that you want the
interpreter to execute. It will automatically search for a file by that name that has
the .class extension. If it finds the file, it will execute the code contained in the
specified class.

No comments:

Post a Comment