In the first part of this tutorial I mentioned that there were two types of Java program, applets and applications. So far we have dealt only with applets, as they are designed to form part of web pages and hence very easy to demonstrate on a web site. The section here explains the essential differences between writing applets and writing applications.
Applications are not designed to run on web pages. They are run using a program called the Java Interpreter. This takes the program as its input and produces a window on the screen in which your program runs. The best way to demonstrate this is with an example.
Enter this into a text file and save the file using the name FirstApplication.java. The file name must match the word after the word class in the program itself (which is FirstApplication).
class FirstApplication
{ public static void main (String args[])
{ System.out.println("Hello, everyone!");
}
}
Note that the structure of the program is slightly different from the equivalent program written as an applet. The program must now be compiled using the Java Compiler, into a form called bytecodes, which the Java Interpreter can run. To use the Java Compiler you will need to be able to type some sort of command line. You may find the MSDOS prompt on a menu somewhere, or you can select Run from the Start menu and type the command in the box that appears. On my machine, I select Run from the Start button menu, and then type the command into the prompt box that appears:
![]() |
![]() |
![]() |
Compile this with javac FirstApplication.java which translates the program into a form called bytecodes and produces a file called FirstApplication.class.
Run the program from the system prompt by typing java FirstApplication. This makes the application appear in its own window, which will be blank apart from the message Hello, everyone!
Unlike applets, in which one gets control of the position where the message appears on the screen, applications display their output one line after another, like text being typed into a text file. The standard method for displaying text is the System.out.println, where the ln part stands for "line" and means that the print position moves to the next line ready for the next line of text to be printed.
Here is a class with two methods:
class MySecondProg
{ public static void main (String args[])
{ politeGreeting("students");
}
public static void politeGreeting (String personName)
{ System.out.println("Hello, " + personName + "!");
}
}
The array args[] represents parameters on the command line, for instance, you can replace "students" in the program above with args[0] as follows:
class MySecondProg
{ public static void main (String args[])
{ politeGreeting(args[0]);
}
public static void politeGreeting (String personName)
{ System.out.println("Hello, " + personName + "!");
}
}
The program is now run by adding a parameter to the program name e.g.
java MySecondProg Sarah
Since the program refers to the parameter, that parameter is now compulsory - otherwise the program would come across args[0] and have nothing to which to match it. You can run a program with more than one parameter, and the program will recognise them using args[0], args[1] etc. The number of parameters is stored in args.length (no square brackets) which, as usual, simply holds the number of elements in the array. The following version of the program can cope with an optional parameter on the command line, as it checks to see whether the parameter is there or not:
class MySecondProg
{ public static void main (String args[])
{ if (args.length == 0)
politeGreeting("students");
else
politeGreeting(args[0]);
}
public static void politeGreeting (String personName)
{ System.out.println("Hello, " + personName + "!");
}
}
Alternatively, you can greet all the names on the command line by inserting a loop in the method main:
class MySecondProg
{ public static void main (String args[])
{ if (args.length == 0)
politeGreeting("students");
else
for (int count = 0; count < args.length; count++)
politeGreeting(args[count]);
}
public static void politeGreeting (String personName)
{ System.out.println("Hello, " + personName + "!");
}
}