Amazon Honor System Click Here to Pay Learn More

by Joseph P Russell

This tutorial is geared to people who have never programmed in Java or are just beginning.

Before I start, it should be noted that none of the following programs can be compiled or run on your system without the use of a Java Interpreter. Go to for more information. I would recommend you download the latest version of the Java Development Kit (JDK), which is currently JDK1.2 or sometimes referred to as Java 2.

The Basics - Java is a strictly object oriented language. It is not possible to write a Java program without defining a class. A class is just a way to bundle data and functions together in a reusable way. We'll go over exactly what object oriented programming (OOP) is in a later chapter, for now, let's just pump out a program!

Object Oriented Programming (OOP) is an efficient way to encapsulate data with related operations into classes. Classes will be discussed later on.

Your first Java program:

<Code> <Program>

Program Listing:

public class Huggable {
   public static void main(String args[]) {
      System.out.println("Game programmers are huggable!");

The program above just prints out a message "Game programmers are Huggable!" As you can see in the image above, the message is displayed at the command line. You can also see the javac command, which is the compile command. *.java files are source code files and *.class files are the actual programs or object files.

This program was run with Windows 95, but could be run, without the need for recompiling, on just about any system (Such as Mac or Solaris). Each system has it's own System dependent Java Interpreter which interprets Java byte code. When a Java program is compiled, it is not compiled into machine language. Instead, it is compiled into Java byte code. This byte code can then be interpreted by any system possessing a Java interpreter.

The first line of code starts the Huggable class definition. You should note the class keyword used to define Huggable as a class. The public keyword is just a way of giving full access to the class. I'll go over the public keyword in a later chapter. Another thing to note in the above snippet of code is the first curly brace {. It represents the beginning of the class definition. The class definition does not end until the corresponding closing curly brace is used }. There may be any number of curly braces in between as long as each opening curly brace has a corresponding closing curly brace. You can see that there are other braces within the class braces. By convention, blocks (surrounded by braces) of code are indented in such a way that the contents of each block is easier to distinguish. Most of the time, I indent 3 spaces for each block deep.

The second line of code is the beginning of the main() method. A method is like a small program that does a specified task called by the main program. To simplify things, if you think of a class, Car, for instance. You could write a go() method which instructs the car to go, but not until you tell it to. Methods will be explained later on so don't worry if you're a little confused at the moment. The most important thing to note here is that the second line defines the beginning of a method, the main() method. The main() method is automatically called first when you run a Java Application. It directs the flow of the program. For now, you can disregard the public static void part. Those words are used to describe the method being defined. The next part is the method name main. The part after the name of the method i.e. the part in parentheses, (String args[]). That part of a method definition is called the arguments. Arguments are values which you send to the method. In this case, the argument is an array of Strings (just a list of words that can be passed to the program from the command line). It is not used in our first program. You should also note that all method definitions are surrounded by curly braces within the class curly braces.

The next line of code is actually a call to a method. It calls the println() method defined within the Java Language. See below for more details. The argument to that method is also a String, which is just a list of characters. The argument in this case is "Game Programmers are huggable!" which is passed to the println() method. This method instructs the system's output (usually a command line) to print that String. You can see the results of passing this argument to that method above. It prints to the screen. The rest of the program are just the closing curly braces.

Here is a more advanced description of the println() method of our program: 
System.out.println("Game Programmers are huggable!");

This line of Java code is a call to the println() method defined in the PrintStream class. It accepts a String and prints it to the System's output stream. The System class is a class which is defined in the java.lang package. It defines the system on which the Java Interpreter is installed. The System class has a PrintStream member called out. When the call to println() is made, you should notice that the name of the System class comes first and is then followed by a dot '.' which is used to separate classes from their members and methods. System.out refers to the out member defined in the System class, which is actually a class itself. out is an instance of the PrintStream class. System.out.println(String) refers to the println() method of the out instance of the PrintStream class which is defined as such in the System class. This may be all very confusing to you, but as you read on, you'll realize how simple and intuitive OOP is.

The next Chapter will be about Classes and General Object Oriented Programming.


Amazon Honor System Click Here to Pay Learn More