Translate

Saturday 9 June 2012

A simple Java program

/*
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 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. 

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 we know, 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 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. 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