Module 1: An Introduction to Java


Language Overview

 
What is Java? Java is an object-oriented programming language with which programs are first compiled and then interpreted:
 
What else comes with Java?

When you "download" Java, you really download a development kit (usually called JDK) that contains:

 
History:
 
What can you do with Java?
 

In early 1990's hype, Java's designers described Java's features as:

Java: A simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high-performance, multithreaded, dynamic language.

Let's examine some salient features (and the hype):

 
Problems with Java (today): Single biggest advantage of using Java: Shorter development time.
 


Our first Java program

 

The source file is a plain text file:

public class HelloWorld {

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

}

To compile at the command-line, use the compiler javac

 javac HelloWorld.java
  

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

To execute (at the command line):

  java HelloWorld
  
where java is the Java interpreter).
 
Exercise 1.1: 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
//
// 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!");

  }

}
  
 
Exercise 1.2: Try executing this once as
  java HelloWorld2
  
and then as
  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)


// A simple application that uses the mouse to draw.

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class MoreComplexExampleSwing extends JPanel implements ActionListener {

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

    public MoreComplexExampleSwing () 
    {
	this.addMouseListener (
           // Unusual syntax for anonymous classes:
           new MouseAdapter () {
  	       public void mousePressed (MouseEvent m)
	       {
		   System.out.println ("mousePressed event: " + m.paramString());
		   int x = m.getX();
		   int y = m.getY();
		   Graphics g = MoreComplexExampleSwing.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 = MoreComplexExampleSwing.this.getGraphics();
		   g.drawLine (currentX, currentY, x, y);
		   currentX = x;
		   currentY = y;
	       }
	   }
       );

    }

    // Button-pressed events:
    public void actionPerformed (ActionEvent a)
    {
	String eventDesc = a.getActionCommand();
	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;
	}
	this.setBackground (c);
	this.repaint();
    }

    void buildGUI ()
    {
        // Example of how a GUI is constructed:
        JFrame frame = new JFrame ();
        frame.setSize (500, 500);
        Container cPane = frame.getContentPane();
        cPane.add (this, BorderLayout.CENTER);

	JPanel bottom = new JPanel ();

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

	Button rb = new Button ("Red");
	rb.addActionListener (this);
	bottom.add (rb);
	
	Button bb = new Button ("Blue");
	bb.addActionListener (this);
	bottom.add (bb);

	cPane.add (bottom, BorderLayout.SOUTH);
	
        frame.setVisible (true);
    }

    // To start execution, "main" is needed
    public static void main (String[] argv)
    {
	// Notice the unusual syntax:
	new MoreComplexExampleSwing().buildGUI();
    }

}

To compile:

 
% javac MoreComplexExampleSwing.java

To execute:

 
% java MoreComplexExampleSwing 
 

Exercise 1.3: Compile and execute the above program.




© 1998, Rahul Simha (revised 2017)