Java Applications


Java programs consist of pieces called classes and methods. Although you can program the classes and methods yourself, you can also utilize collections of existing classes and methods in Java class libraries. Thus, there are really two pieces to learning Java--learning the Java language itself so that you can program your own classes and methods, and learning how to use the classes and methods in the Java class libraries. Class libraries are provided both by compiler vendors and independent software vendors. 

Java systems generally consist of several parts: an environment, the language, the Java Applications Programming Interface, and various class libraries. 


An Example Application 

 1  // A first application in Java
 2  public class Welcome1 {
 3     public static void main (String args[ ])
 4     {
 5         System.out.println("Welcome to Java Programming");
 6     }
 7  }

Note that the line numbers are not part of a Java program, but have been added for reference.

Various components of the above program will be discussed below.

For a Java application class, one of the methods must be called main and must be defined as shown in line 3, otherwise the Java interpreter will not execute the application.

An application begins execution at the main method (line 3).  Applications are executed using the Java interpreter (java). 


Comments

Compiled or Interpreted???

Compiling the Program

A program has to be converted to a form the Java VM can understand so any computer with a Java VM can interpret and run the program. Compiling a Java program means taking the programmer-readable text in your program file (also called source code) and converting it to bytecodes, which are platform-independent instructions for the Java VM.

Interpreting and Running the Program 

Once your program successfully compiles into Java bytecodes, you can interpret and run applications on any Java VM, or interpret and run applets in any Web browser with a Java VM built in such as Netscape or Internet Explorer. Interpreting and running a Java program means invoking the Java VM byte code interpreter, which converts the Java byte codes to platform-dependent machine codes so your computer can understand and run the program.
 

Classes


A modified program

Rather than making use of the command window, most Java applications use windows or dialog boxes to display their output.

 1  // Printing multiple lines in a dialog box
 2  import javax.swing.JOptionPane;   // import class JOptionPane
  3
  4  public class Welcome2 {
 5     public static void main (String args[ ])
 6     {
 7         JOptionPane.showMessageDialog (
  8                   null, "Welcome\nto\nJava\nProgramming");
  9
10         System.exit (0);   // terminate the program
11     }
12  }

Import

The exit method is required in any application that displays a GUI in order to terminate the application.

The 0 argument passed to the exit method indicates that the application terminated normally.

Identifiers that start with capital letters normally represent class names, so you can assume that System is a class.

Class System is part if the package java.lang, but does not require an import statement because it is imported automatically into every Java program.


Recap:

Java applications require the statement public static void main (String args[ ]).

Java applications display their output in a command window unless a dialog box or other type of window is specified.

Java applications that use the GUI must include the line System.exit(0);