©1994-2003 Kevin Boone
Home     Section index     K-Zone home Simple Java programs

Site search

Glossary
Confused by computer jargon? Look it up!

Shameless plug


Now available!

Articles
- Ten-minute guide to setting up a WAP site

- Talk like your boss: new developments in managerese

More...

Development
File handling in the Linux kernel

Java development for the Sony-Ericsson P800

SunONE Application Server 7 FAQ

More...

Linux
Using Linux with the Treo 600

- Linux on the Tecra M1

- Some notes on openzaurus

More...

Download
Java stuff

Linux stuff

More...

(Please read the download policy)

Home automation
The X10 system

Linux TW723 driver

More...

The K-Zone
K-Zone computing

K-Zone law

K-Zone education and science

K-Zone motorcycles

K-Zone DIY

K-Zone railways

K-Zone martial arts

About the author

K-Zone home page

 
Software development
Computing
Add1.java
Add1_1.java
AwtTest.java
Button1.java
Button2.java
Calculator.java
CalculatorApp.java
ClockApplet.java
CountSpace1.java
DateTime.java
Div1.java
DoNothing.java
Factorial1.java
Factorial2.java
FontChooser.java
IfTest.java
Inheritance1.java
Loan.java
DiceThrower.java
MultiplicationTable.java
Queue.java
Reverse1.java
SoundApplet.java
Stressometer.java
TextReaderTest.java
WebMerge.java
Zener.java
`Hello1' example program: very simple `Hello world' program

Hello1.java

// This is a `comment'. You can tell because the
//  lines start with `//'. These lines are all
//  ignored by the Java compiler, so you can write
//  whatever you like. At the very least you should
//  start with a comment saying what the program
//  does


// Note that this program has far more comments than
//  real program statements; this would probably not
//  be the case in most real programs.

// Hello1.java
// A program that displays a message
// Kevin Boone, May 1999

// The first real (i.e., non-comment) line of the program is `import'
// This tells the compiler what classes to use apart
//  from any defined in this program. In this case
//  I tell the compiler to consider using any of the
//  classes that are part of the `package' java.awt.
//  and the class `Applet'
// I have to do that because the program uses the
//  Class `Applet' to do most of the work, and this
//  class is defined in the java.applet package and
//  the java.awt package. 
// Most Java programs will start with an `import'
//  statement

import java.applet.Applet;
import java.awt.*;

// So now down to work. Define a class. All java programs
//  have at least one class. In this case we are writing an
//  applet, so the new class is a type of applet. `extends'
//  means, essentially, `is a type of'.
// Note that Java rules stipulate that a `public' class
//  must be defined in a file of the same
//  name, i.e., Hello1 must be defined in `Hello1.java'

public class Hello1 extends Applet
// The open brace below denotes that all the statements that
//  follow are part of the class Hello1, until the matching
//  closing brace at the end of the program
{

// Now we define an operation called `paint'. Providing this
//  operation ensures that when the program starts up,
//  something useful will happen. 
// If we do not provide a `paint' operation, then the program
//  will compile and run correctly, but it won't display
//  anything at all.
// The concept of an `operation' will be covered in more detail
//  later in the course
public void paint (Graphics g)
	{
	// Now the program text is `indented', this is,
	//  all the lines start a few spaces from the
	//  left margin. Doing this makes it easy for the
	//  (human) reader to identify all the lines that 
	//  are part of `paint'. The computer does not
	//  care about this, but people will find the
	//  program easier to understand

	// `drawString' is an operation in the class called
	//   Graphics. Its job is simply to display text in
	//  an area of the screen. In this case the text is
	//  positioned 20 pixels across, and 20 pixels down	

	g.drawString ("Welcome to INT4120", 20, 20);
	}

// This final brace denotes the end of the class `Hello1' and,
//  in this case, the end of the program
}