Learning Java
Custom Search

Previous Next

Application vs Applet

In this tutorial, we will study 2 types of Java programs, the application and the applet. There are other types not covered in this tutorial, one of which is the servlet, used on web servers.

    Java Application - is a standalone program or set of programs that contains a main method which defines the entry point of the program.
    Java Applet - is a program that requires a web page to run.

Java Application

Note the basic structure of an application. It requires the class keyword with the name of the application. This name must be the same as the name of the text file. If there are multiple classes in one text file, the text file should have the same name as the class that is the entry point for the application, usually the class that containes the main method.

The main method is required for applications. It is the main method that gets executed first when the application is run.

The following is an example of an application. The class is called HelloName, therefore, the file that it would be saved to would have to be HelloName.java. As required, it has a main method. The main method must have the following modifiers in front of the word main.

    public - allows main to be accessed by the java interpreter, the JVM.
    static - an object of this class does not have to be instantiated before this main is used.
    void    - main will not return a value to the caller.
class HelloName {
    public static void main(String args[]) {
	System.out.println("Hello, " + args[1] + ", " + args[0]);
    }
}

This application uses the System class to produce output. The field out represents the "standard" output stream. The method println writes the contents of the parentheses to the standard output device which is probably the monitor.

The main method has a parameter, in this case, args[]. The parameter can have any name but must be declared as String and must be an array, indicated by []. It is not required that the parameter be actually used in the code. In this example, we are using the parameter and expecting the FirstName and SecondName to be entered for args. The general format for execution follows:

    java classname param1, param2, param3, ...

Prior to execution, we, of course, must compile the application and create our .class file using:

    javac HelloName.java

To execute our HelloName.class, the user might enter the following.

    java HelloName George Washington
    java HelloName John Doe

In the above illustration, we moved into the directory where our source was stored. Then we compiled the class using javac.

In the first execution, we did not enter our parameters and got an error. In the System.out.println(..), we had referenced the parameters, args[1] and args[0], so we have to present them in our execution. Arrays like args[] will be explained in the section on arrays later in this tutorial.

Previous Next

Steps In Learning

Contact us

Copyright © 2008      N. Nelson      All Rights Reserved