Java Tutorial for Beginners

 

What do I need to begin developing in Java?

You need a Java Development Kit (JDK), which is downloadable for free from www.java.sun.com. Some operating systems, such as certain versions of Linux and Unix, come bundled with a copy of the JDK. Lots of people prefer to work via an IDE (Integrated Development Environment) such as NetBeans or Eclipse, since this gives you a simple, menu-driven environment for development. Eclipse and NetBeans are both available fro free on he Internet. If you don't have an IDE, you can work from the Unix, Linux or DOS command prompt, using any text editor to create your programs.

 

Creating, Compiling and Running a Java program

In this tutorial, I used the Eclipse Europa 2007 IDE for most of the programming examples. I'll also take a quick look in this chapter at how to create, compile and run a program from the Unix, Linux or DOS command prompt. If you have a different IDE, you should be able to find corresponding menu choices quite easily.

Here's the code for a simple Java program that simply displays the text "I am a Java guru!" on the screen.

public class Guru
{

    public static void main(String[] args)
    {
        System.out.println("I am a Java guru!");
    }

}

To create and run this program in Eclipse:
You should have the Eclipse Workbench open. The screen should look like this:

In Eclipse, Java programs are grouped together into projects. Before entering the first sample program, we need to create a project called MyExamples. This will be used to store all the examples from this tutorial. Choose File, then New, then Project from the menu. You will be presented with a screen that looks like the one below. All you need to do is enter the name of the project in the appropriate box, then click the Finish button to create the project.

Now you need to create the Java class Guru. Choose File then New then Class from the menu. You will see a screen like the one below. Fill in the name of the class (Guru), and, under the section 'Which methods would you like to create?', select the checkbox next to 'public static void main'. Then click 'Finish'.

You will now see the new class appearing in the central window as below.

You will notice that it has generated most of the code for the program Guru.java. It has also generated some comment lines. The comment lines enclosed between /** and */ form part of the program documentation. You normally edit these to contain meaningful comments about the class. Single-line comments begin with //. These are notes for the programmer. They are usually used to explain the code so that anyone who has to work on it later knows what each section of code is for.

You can now use the center window as a text editor, and add in the rest of the program. It is an 'intuitive' editor; in other words, it tries to anticipate what is likely to come next in a Java program. It also highlights any errors with a wavy red line. This takes a bit of getting used to, but is quite simple to use. Edit it so it looks like this:

Note that Java is case-sensitive, and punctuation symbols as semi-colons and brackets are important. Save the text by selecting Save from the File menu, then choose Run from the Run menu. If you made any mistakes, it will tell you that there were errors in the class, and highlight them. If this happens, fix the errors and try again.

If there were no errors, the program will run, displaying its output in the Console window at the bottom of the screen as below:

If you are working from the command prompt rather than through an IDE, you will need to carry out the following steps instead. Assuming that Java has been correctly installed on your machine:

Use a text editor such as Notepad in Windows or vi in Unix or Linux to place the program text shown below into a file named Guru.java. The name of the source file and the name of the public class must always be the same. Names are case sensitive. If using Notepad, save as type 'All files' rather than type 'Text Files'. If you don't, it will add an extension of .txt on the end, which will confuse the Java compiler.

public class Guru
{

    public static void main(String[] args)
    {
         System.out.println("I am a Java guru!");
    }

}

Compile the program by typing

javac Guru.java

If you made any mistakes, error messages will be displayed on the screen. If this happens, go back and fix your program so it looks exactly like the text shown above. Be careful of puctuation, and remember that it's case sensitive.

Once it's compiled successfully, you can run it by typing:

java Guru

It should then display the message 'I am a Java guru' on the screen.

You have written, compiled and run your first Java program!

Let's use this simple program to learn a few things about Java. Don't worry if some things are a bit confusing at present: it will all become clearer later.

  1. In Java, everything is written as a class, and has a name. The first line of the program:

    public class Guru

    is used to introduce the class and give it a name, in this case Guru. The word 'public' is a modifier - it gives more information about the class. public means that it can be accessed from other programs. This line is called a class header.

  2. 2. Classes can contain methods. In this program, there is a method named main, introduced with the method header:

    public static void main(String[] args)

    Method headers can also contain modifiers. public means that this method can be called by other classes. We'll look at the meaning of static in a later chapter

    Methods can sometimes pass an item of data back to the class that called them. The method header must either specify what kind of data the method will return, or use the word void to specify that it will not return any data. This is known as the return data type of the method.

    Each method must have a name. In this case, the method is called main. If a Java class is to be run as a stand-alone program, it must have a method called main. This becomes the starting point of the program.

    Some methods will need to receive data from the class that calls them. The method header must define the number and type of data items that it will expect, and give each one a name. These data items are known as arguments to the method. This particular method expects an array of strings, given the name args. For those of you who have not done much programming, you can think of an array as being a collection of similar items. A program's main method should always expect an array of strings. These can be supplied by the user on the command line when the program is run. Even if the program doesn't use them, you should still define them.

  3. Braces {} are used to enclose blocks of code. In this program we have an inner and an outer block. The outer block consists of everything that makes up the class Guru. The inner block encloses everything that belongs to the method main.

  4. The following line:
    System.out.println("I am a Java guru!");

    defines an action to be taken. In later chapters, you'll learn more about how to code actions (or commands). For now, just use this command as it is (Other than changing the text between the quotes) every time you want to display something on the screen.

This program can be used as a skeleton for every stand-alone Java program that you write. Try experimenting with it to create a program of your own that displays a different message, or several messages.