Rob Kremer

Java Tutorial

About Java

"Hello World" Application

(This program is from (Campione & Walrath 1996).)
 1  /**
 2   * The HelloWorldApp class impliments an application that
 3   * simply displays "Hello World!" to the standard output.
 4   */
 5  class HelloWorldApp {
 6    public static void main(String[] args) {
 7      System.out.println("Hello World!"); //Display the string
 8      }
 9    }


Produces the file HelloWorldApp.class containing bytecode


The class in run in the Java interpreter: Use the name of the class, not the the name of the file. 

"Hello World" Applet

(This program is from (Campione & Walrath 1996).)
 1  import java.applet.Applet;
 2  import java.awt.Graphics;
 4  class HelloWorld extends Applet {
 5    public void paint(Graphics g) {
 6      g.drawString("Hello World!", 50, 25);
 7      }
 8    }


Produces the file HelloWorld.class containing bytecode.

But it's not an application, it's and applet, so we need to run it in a browser.

The HTML File

Name the .java (and .class) file with the same name as the class. Put the .class file in the same directory as the HTML file.

When the above HTML file is viewed in netscape, the HelloWorld class's paint method is called:

View HelloWorld.html.


Java Primitive Types
Type  Size/Format 
byte  8-bit two's complement 
short  16-bit two's complement 
int  32-bit two's complement 
long  64-bit two's complement 
float  32-bit IEEE 754 
double  64-bit IEEE 754 
others types
char  16-bit Unicode char 
boolean  true or false

Flow Control Statements

Arrays and Strings

 1  int[] arrayOfInts = new int[10];
 3  for (int i=0; i ≤ arrayOfInts.length; i++) {
 4    arrayOfInts[j] = j;
 5    System.out.println("[j] = " + arrayOfInts[i]);
 6    }
 1  String file;
 2  file = "Input";
 3  file = file + ".txt";
Can throw ArrayIndexOutOfBoundsException or NullPointerException (for uninitialized object or array in an array) 

Objects and Classes

See also Objects, Classes, and Interfaces in Sun's Java Tutorial.

Very similar to C++. Exceptions:

Class declaration

[ modifiers ] class className
      [ extends superClassName ]
      [ implements InterfaceNames ] {

Member variable declaration

[accessSpecifier] [static] [final] [transient]
[volitile] type variableName

Method declaration

[accessSpecifier] [static] [final] [native]
[synchronized] type methodName ([paramList])
  [throws exceptionsList]


[public] interface interfaceName
      [ extends listOfSuperInterfaces ]

Access specifiers

Specifier  class  subclass  package  world 



Try the above examples. Then copy them to your own directory and modify them in interesting ways. There is lots of on line documentation on Java, so it shouldn't be hard to make trivial modifications like randomized colors on Nervous Text, or changing the timing on the waving wizard. Use you imagination.

449 students now have permission to use fsi. You must use fsi, and set your path to include /usr/java/bin to be able to use javac. You can copy the source from my web directory, as well as the html.

Have fun! 


Arnold, K. & Gosling, J. (1996). The Java Programming Language, Addison-Wesley.

 Campione, M. & Walrath, K. (1996). The Java Tutorial: Object-Oriented Programming for the Internet. Reading, Massachusetts, Addison-Wesley.

 Flanagan, D. (1996). Java in a Netshell. Bonn, O'Reilly & Associates.

 Sun Microsystems Inc. (1997). Java API Documentation (version 1.0.2)., February 4, 1997.

 Sun Microsystems Inc. (1997). The Java Tutorial., February 20, 1997.

 Sun Microsystems Inc. (1997). The Java Language Specification., February 11, 1997.

UofCSoftware Engineering Research Network, Department of Computer Science

Rob Kremer