Start Here

index
BASIC

There are two basic kinds of Java programs. An application is a stand-alone program, like just about every other program you have used. An applet is a program that is put on a web page, and is executed by your browser.

Applications

Here is the basic structure of an application (// indicates the places that your code goes):

// If you use a package statement, it goes here
 
// Your import statements go here
 
public class MyApplication {
    // Declarations of your instance variables
 
    public static void main(String[] args) {
        // Your statements go here
    }
}

While this is the minimum structure you must have, it has the problem that practically every field and method you write must be declared static. A better basic structure is as follows:

// If you use a package statement, it goes here
 
// Your import statements go here
 
public class MyApplication {
    // Declarations of your instance variables
 
    public static void main(String[] args) {
        MyApplication myApp = new MyApplication();
        myApp.run();
    }
 
    void run() {
        // Your statements go here
    }
}

In the above, MyApplication, myApp, and run may be replaced by any names you choose. This structure avoids certain problems with static variables that can be very confusing to beginners.

Applets

Here is the basic structure of an applet (// indicates the places that your code goes):

// If you use a package statement, it goes here
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// Any additional import statements go here
 
public class MyApplet extends JApplet {
    // Put declarations of your instance variables here
 
    public void init() {
        // this is where you create your GUI (buttons, text fields,
        //  etc.) and do any other necessary initializations
    }
 
    public void start() {
        // (usually not needed) put here any code required to run
        // each time web page is visited 
    }
 
    public void stop() {
        // (usually not needed) put here any code to be run when
        // the browser page is replaced by another page or before
        //  JApplet is destroyed
    }
 
    public void paint(Graphics g) {
        // this is where you do any drawing other than standard GUI
        // elements (buttons, text fields, etc.)
    }
 
    public void destroy() {
        // (usually not needed) put here any code to be run when
        // the applet is about to be destroyed
    }
}

Now what?

If you have not installed the necessary software on your computer, you should do so now.

You could begin with the basic application (not applet), and try the following.

Put in some print statements and print a few lines.

Declare some variables and print them out.

Do some arithmetic on your variables, and print out the results. This involves understanding a little about both assignment statements and arithmetic expressions.

Use if statements to decide whether or not to print out certain things. This will require using simple conditions, and possibly some more complex boolean expressions.

Use a while loop to print out several copies of a line.

Use a while loop combined with assignment statements to print out lines that vary. For example, you could print out the numbers 1 through 100.

Write some simple methods to break up your program into logical parts.

Learn to use String methods to do some simple computations with text. A String is one of the kinds of object that Java provides for you.

If, after all this, you can write some simple classes of your own, and create and use objects of those classes, you are well on your way to understanding Java.