Introduction to Java

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

Applets - IE 3.0+, Netscape 3.0+, Hotjava

Availability

Language

Versions

Examples

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
  edit Hello.java
Import packages as needed.
  import java.io.*;      // to read/write files
  import java.net.*;     // 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).
  javac Hello.java
Run your program
  java Hello
  Hello world

Compiling and Running Applets

Create a text file with a .java extension (as before)
  edit HelloApplet.java
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() {
    setBackground(Color.white);
    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).

  javac HelloApplet.java
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:

Examples

Standalone programs

Scribble3.java 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) {
    this.x=x;
    this.y=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);
    radius=r;
  }
  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
  }
}


Threads