Module 1: An Introduction to Java


Language Overview


Our first Java program: Helloworld

The file is a plain text file:

public class HelloWorld {

  public static void main (String[] argv)
  {
    System.out.println ("Hello World!");
  }

}

To compile, use the compiler javac (This is for Unix, don't type the percentage symbol):

 % javac HelloWorld.java

This should produce a file called HelloWorld.class (the executable bytecode to be interpreted) in the same directory.

To execute (java is the Java interpreter):

 % java HelloWorld 

In-class exercise: Try it out!


Helloworld - a closer look

We will now look at HelloWorld2.java, which similar to HelloWorld.java but has more explanation:

// File: HelloWorld2.java
// Author: Rahul Simha
// Date created: August 14, 1998.
//
// Note how in-line comments start with //
// I will follow the above convention of filename, author etc.

public        // Accessible outside this file. "public" is a reserved word.
  class       // "class" is a reserved word.
  HelloWorld2 // The name of the class - same name as filename.

{             // Braces instead of begin/end.

  public      // This function must be accessible. "public" is reserved.
    static    // "static" is reserved.
    void      // The return value - nothing. "void" is reserved.
    main      // To start execution, a class must have a "main" function.
              // "main" is sort-of reserved.
    (String[] argv)  
       // Command line parameters are placed by the run-time system
       // in argv[0], argv[1], etc.
  {

    System.out.println ("Hello World!");
    // System.out is an object that contains a useful function
    // called println, which takes in a string and prints it out on
    // a new line.

    System.out.println ("Here's the list of arguments you gave me:");

    for (int i=0; i < argv.length; i++)
      System.out.println ("Argument#" + i + ": " + argv[i]);
    // Notice how strings can be concatenated with the `+' operator.
    // Is `i' a string or a variable?

    System.out.println ("Bye!");

  }

}

Try executing this as either (Unix):

   % java HelloWorld2 

or

   % java HelloWorld2 I dont know whats going on


A more complex example


Just to get a feel for a real Java program, let's look at a more complex example: (source file)


import java.awt.*;
import java.awt.event.*;

public class MoreComplexExample
  extends Frame 
  implements ActionListener {

  // Store current location of mouse
  int currentX=0, currentY=0;

  // Peculiarities of the Java GUI model -- listener objects
  public void createListeners ()
  {
    this.addMouseListener (
      new MouseAdapter ()
      {
	public void mousePressed (MouseEvent m)
	  {
	    System.out.println ("mousePressed event: " + m.paramString());
	    int x = m.getX();
	    int y = m.getY();
	    Graphics g = MoreComplexExample.this.getGraphics();
	    g.drawLine (currentX, currentY, x, y);
	    currentX = x;
	    currentY = y;
	  }
      }
    );

    this.addMouseMotionListener (
      new MouseMotionAdapter ()
      {
	public void mouseDragged (MouseEvent m)
	  {
	    System.out.println ("mouseDragged event: " + m.paramString());
	    int x = m.getX();
	    int y = m.getY();
	    Graphics g = MoreComplexExample.this.getGraphics();
	    g.drawLine (currentX, currentY, x, y);
	    currentX = x;
	    currentY = y;
	  }
      }
    );

  }

  // Button-pressed events
  public void actionPerformed (ActionEvent a)
  {
    String eventDesc = a.getActionCommand();
    System.out.println ("Button event: " + eventDesc);
    Color c = Color.gray;
    if (eventDesc.equalsIgnoreCase ("quit")) 
      System.exit(0);
    else if (eventDesc.equalsIgnoreCase ("red")) 
      c = Color.red;
    else if (eventDesc.equalsIgnoreCase ("blue")) 
      c = Color.blue;
    else if (eventDesc.equalsIgnoreCase ("green")) 
      c = Color.green;
    this.setBackground (c);
    this.repaint();
  }

  // Test the code via a static function
  public static void test ()
  {
    MoreComplexExample f = new MoreComplexExample ();
    FlowLayout fl = new FlowLayout();
    f.setLayout (fl);

    Button qb = new Button ("Quit");
    qb.addActionListener (f);
    f.add (qb);

    Button rb = new Button ("Red");
    rb.addActionListener (f);
    f.add (rb);

    Button bb = new Button ("Blue");
    bb.addActionListener (f);
    f.add (bb);

    Button gb = new Button ("Green");
    gb.addActionListener (f);
    f.add (gb);

    f.createListeners ();
    f.setResizable (true);
    f.setSize (500,600);
    f.setVisible (true);

  }

  // To start execution, "main" is needed
  public static void main (String[] argv)
  {
    test ();
  }

}

To compile:

 
% javac MoreComplexExample.java

To execute:

 
% java MoreComplexExample 

In-class exercise 2: Try executing the program.