Lecture Summary 1

Introduction to Java


Installing JDK 1.3 or higher (required)


Installing BlueJ to write/compile/run the program (optional)

  • Download a development environment program called BlueJ from http://www.bluej.org/
  • Make sure JDK is installed
  • Follow the installation instruction
  • Download the BlueJ Tutorial
  • Other instructions (prepared by Noriko) can be found here .

  • Installing TextPad to write/compile/run the program (optional)

  • Download TextPad
  • The TextPad java FAQ
  • More Info on setting up TextPad

  • Your first Java program

    We introduce two simple Java programs: an app (for application) and an Applet to illustrate the basic structure of a Java program and see how we compile and run a Java program.

    1. A Simple App:

    This program to displays a greeting message sent to to the standard output. The steps to build and run the app are as follows, assuming that you have installed the Java Development Kit on your system.
  • Source code: HelloWorld.java

  • Type in and save the source code in a file named HelloWorld.java.

  • Compile: javac HelloWorld.java

  • Compile the source code, assuming that the PATH is set correctely. If the compilation is successful, a file named HelloWorld.class will be generated. This is the Java byte-code file.

  • Run: java HelloWorld

  • Execute the app by invoking the Java byte-code interpreter (resides in c:\jdk1.3\bin\).

    2. A Simple Applet:

    Java apps must be invoked from command consoles using the Java interpreter. Java applets can be embeded in HTML Web pages. They are invoked by Java enabled browsers (Web browser or appletviewer). Let's consider a simple applet to illustrate the basic applet structure. This applet displays the greeting message graphically. The JAva byt-code (NOT source code) is linked to an HTML document.

    Graphics and Coordinate Systems

    Each point in the Java coordinate is represented by (x,y). The top-left corner of drawing area is (0,0). The x increases as you move right and y increases as you move down.
  • Type in the source code:
    HelloWorldApplet.java

  • Compile the source code:
    javac HelloWorldApplet.java

  • Type in and save the following HTML source in a file named HelloDemo.html.
  • One way to view th eapplet is to use a Java-enable browser such as IE or Netscape
  • Another way to view th eapplet is to use the applet viewer in JDK
    appletviewer HelloDemo.html
  • Basic Applet Structure

    Java basics

  • Examples: