Week 1: Java Overview

What is Java ?

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

 

Why Java ?

 

Background

 

Java Features

 

What is a Java Program called ? - Applications vs. Applets

Applications Applets
loaded from local or network storage downloaded from server as needed
runs on platform directly runs on client within browser
usually larger usually smaller
unlimited file access limited file access
unlimited network access limited network access
entry point: main() method entry point: extends java.applet.Applet class

 

Basic Development Concept

Traditional compiled programs

Stage 1 Stage 2 Stage 3
  Compiler (Pentium) Binary File
(Pentium)
Your Code

void main()

{
...
}

Compiler (PowerPC) Binary File
(PowerPC)
  Compiler (SPARC) Binary File
(SPARC)

Java Programs

Stage 1 Stage 2 Stage 3 Stage 4
  Java Compiler / javac
(Pentium)
  Java Interpreter
(Pentium)
Java Code

class Hello
{
...

}

Java Compiler / javac
(PowerPC)
Java Bytecode
(Platform Independent)
Java Interpreter
(PowerPC)
  Java Compiler / javac
(SPARC)
  Java Interpreter
(SPARC)

 

Java Developers Kit (JDK)

  • Current release: 1.1.5 available for download (We are using 1.1.2)

    You can download the JDK for free from http://java.sun.com or install it from the CD that comes with the text book.

    The JDK includes:

    The JDK does not include:

    The only pieces of the JDK that are needed to run Java code is the JVM and the libraries. The rest is for development only.

    Java files

    CLASSPATH environment variable

    The CLASSPATH environment variable contains a list of directories, .zip files, and .jar files where Java classes are to be found. To display the CLASSPATH in Windows 95:

    echo %CLASSPATH%
    

    To change the CLASSPATH in Windows 95:

    set CLASSPATH = c:\jdk\lib\classes.zip;c:\myclasses
    

    Other operating systems/environments will have different methods of setting the CLASSPATH, but all with honor the notion of the CLASSPATH.

    Other tools

    Most browsers can run Java applets. Some examples are Netscape Navigator and Microsoft Internet Explorer.

    Integrated Development Environments (IDEs) contain the same types of tools as the JDK, but provide a graphical user interface and editor. Some examples are IBM Visual Age for Java, Symantec Cafe, and Microsoft Visual J++.

    For the best documentation, get it online from:
    http://www.javasoft.com/products/jdk/1.1/docs/


    Using the JDK to create a simple application

    1. Start a DOS prompt.
    2. Create a directory: mkdir C:\JavaWeek1
    3. Make that directory the current directory: cd C:\JavaWeek1
    4. Create and edit a new file called Week1.java to contain the Java source code. Since the JDK does not include an editor, use Windows Notepad: notepad Week1.java
    5. Type the Java source code:
          public class Week1
          {
              public static void main (String[] args)
              {
                  System.out.println ("Hello World");
              }
          }
          
    6. Save the file. Choose the File menu, Save.
    7. Back in the DOS prompt, compile Week1.java: javac Week1.java. If you see error messages, then check that you typed the Java source code correctly and fix any typos.
    8. The compiler should have created the file Week1.class. You can verify this: dir Week1.class
    9. Now run the Java application: java Week1. You should see the text "Hello World" on the screen.

    Using the JDK to create a simple applet

    1. Create and edit a new file called Week1Applet.java to contain the Java source code. Use Windows Notepad: notepad Week1Applet.java
    2. Type the Java source code:
          public class Week1Applet
          extends java.applet.Applet
          {
              public void paint (java.awt.Graphics g)
              {
                  g.drawString ("Hello World", 5, 25);
              }
          }
          
    3. Save the file. Choose the File menu, Save.
    4. Back in the DOS prompt, compile Week1Applet.java: javac Week1Applet.java. If you see error messages, then check that you typed the Java source code correctly and fix any typos.
    5. The compiler should have created the file Week1Applet.class. You can verify this: dir Week1Applet.class
    6. An applet is usually embeded in a web page. Let's create a simple web page using HTML. Create and edit a new file called Week1Applet.html to contain the HTML source. Use Windows Notepad: notepad Week1Applet.html
    7. Type the HTML source:
          <applet code=Week1Applet.class width=300 height=300> 
          </applet> 
          
    8. Save the file. Choose the File menu, Save.
    9. Run the Java applet using appletviewer: appletviewer Week1Applet.html.
    10. The applet will run inside its own window.
    11. Note:
      drawString public abstract void drawString(String str,
      int x,
      int y)

      Draws the text given by the specified string, using this graphics context's current font and
      color. The baseline of the first character is at position (x, y) in this graphics context's
      coordinate system.

      Parameters:
      str - the string to be drawn.
      x - the x coordinate.
      y - the y coordinate.

  • Copyright 1996-2001 OpenLoop Computing. All rights reserved.