Introduction to Java

Standalone applications - Windows 95+, UNIX/X, Macintosh

Applets - IE 3.0+, Netscape 3.0+, Hotjava





Java vs. C++

Language features

Standard packages

Java 1.0

Java 1.1

Java 2

Compiling and Running Standalone Applications

Create a text file with a .java extension
Import packages as needed.
  import*;      // to read/write files
  import*;     // to send/receive over network
  import java.util.*;    // data structures (vector, hashtable) or dates
  import java.awt.*;     // if using a graphical user interface
Create a public class with the same name as your source code file.
  public class Hello {
Add a method main exactly as follows, where your program will start.
    public static void main(String[] args) {
Add your code.
      System.out.println("Hello world");
    } // end of main
  } // end of MyProgram
The completed program looks like this. Compile your program in a shell window or MS-DOS box (produces Hello.class).
Run your program
  java Hello
  Hello world

Compiling and Running Applets

Create a text file with a .java extension (as before)
Import the following at a minimum.
  import java.applet.*;
  import java.awt.*;
Extend the class Applet, matching your source code file name.
  public class HelloApplet extends Applet {
Do not write main(). Override init(). This will be the first method called when the applet starts up.
  public void init() {
    setFont(new Font("Serif", Font.BOLD, 24));
Override paint(). This will be called whenever the applet needs to be redrawn (such as when starting up or uncovered by a window).
  public void paint(Graphics g) {
    g.drawString("Hello World", 50, 100);
You will rarely need to override start(), stop(), or destroy(). The finished code will look like this.

To compile (as before).

Create an HTML file with an APPLET tag. The tag should specify the image size (in pixels), .class file name, and codebase (if not in the current directory).
  <APPLET WIDTH=300 HEIGHT=200 CODE="HelloApplet.class"></APPLET>
Test using appletviewer on the HTML file.
  appletviewer HelloApplet.html
Place the HTML and .class file in the same directory on your web server.

Graphical User Interfaces

A GUI program or applet consists of two parts:


Standalone programs is like Scribble2, modified to run as a standalone program.

Object Oriented Programming

interface Drawable {
  public void draw();

class Shape implements Drawable {
  private int x, y;
  public Shape(int x, int y) {
  public Shape() {
    this(0, 0);
  public int getX() {return x;}
  public int getY() {return y;}
  public static Shape makeShape(int x, int y) {
    return new Shape(x, y);
  public void draw() { /* overrides Drawable.draw() */ }

class Circle extends Shape {
  private int radius;
  public Circle(int x, int y, int r) {
    super(x, y);
  public Circle() {
    this(0, 0, 0);
  public int getRadius() {return radius;}
  public void draw() { /* overrides Shape.draw() */ }

public class MyProgram {
  public static void main(String[] args) {
    Drawable d=Shape.makeShape(1, 2);
    d.draw();  // draw a shape
    d=new Circle(1, 2, 3);
    d.draw();  // draw a circle