HelloWorld.java

Introduction

Welcome to the first of my introduction to Java tutorials. These tutorials are aimed at complete beginners to programming. Java is a good language to start with because its syntax is very similar to the more powerful c++ and it has been designed in a very object oriented way. Object oriented programming (or OOP for short) is a way of programming that breaks the problem down into parts. These parts then slot together to form the solution.

What You Will Need

To follow these tutorials all you need to get your hands on is the Java SDK (sometimes called the JDK). SDK stands for Software Development Kit. The Java SDK is available free!! from here . This kit contains all you need to program in java. To write your source files I recommend using a program such as notepad or Xemacs (under *NIX environments). These are just suggestions and any plain text editor can be used as long as it can save files in raw text format.

Main Parts of the Java SDK

The SDK contains many programs and is quite a hefty download. The main parts you will be using are detailed below.

    • javac.exe – This is the java compiler. It turns your source code into files that can be read by the java interpreter.
    • java.exe – This is the java interpreter. It reads through you compiled code line by line and executes the statements.
    • appletviewer.exe – This will be used later on in the tutorials when we get into applet programming. This tool displays an applet as it would appear on an html page. (An applet is a java program that can run in a web browser).

HelloWorld.java

Ok enough explanation, lets start coding! This is the most basic of programs and all programming tutorials and courses I’ve seen seem to start with it.

Fire up your text editor and enter the code listed below and save it as HelloWorld.java.

After saving this file open an ms-dos command box (Start->Run->command.com should do the trick, on newer versions of Windows try cmd.exe) or a *NIX terminal and change to the directory where you saved the source file. To compile a java source file we use javac, the java compiler. So to compile out HelloWorld.java program execute the following statement:  javac HelloWorld.java. If you copied the code exactly right as shown above the compiler won’t say a thing and you’ll just end up with a new command prompt. This is good!!! Now to run your program use the java interpreter (java.exe) by executing java HelloWorld. The computer now reads your file and should print “Hello World!!!” before leaving you at a fresh command prompt. Thats all there is to it. I realise this isn’t very impressive but its essentially one line of code!.

Lets take a closer look at the source code.

This line of code defines a new java class called HelloWorld. A class is a java program. Note the capitalisation of the HelloWorld. To make this file compile it must be stored in a file called HelloWorld.java with exactly the same name (capitalisation etc). The { at the end signifies the start of a block of code in java and as you can see on the last line this block is ended with a }

This is the main part of the program. It is called the main method. This method is always the first thing that is executed in a java application so everything in here is very important. Once again a { must be used to start the main method block. This means that everything that follows between the { and } belongs to the main method.

This line is a call to the println method of the System.out class. Don’t worry about the technicalities, just remember that to print something to the screen you use System.out.println(“<what you want to print>”); The speechmarks indicate that you want to print a string of text. This means that java will print exactly what is in the speechmarks.

Getting User Input

Welcome to the second of my Java tutorials. In this tutorial I am going to go over how to obtain the user’s input. As you may realise this is quite an important topic as if you didn’t know how to do this, your programs would be very boring!

Fire up your text editor of choice and enter the following program:

This code looks more like it! Now remember to save the file as UserInput.java (nothing else will do!!) and compile it using javac. Now run it. If you get any unexpected results check over the example code line by line and see where the error is.

Now lets dissect the code:

This is the first time you have met an import statement in your Java travels. Basically what import allows you to do is include all kinds of useful functions and methods provided by other people (or yourself). These are stored in other java programs or libraries. In this case you are importing the Java Input Output library. This library is provided with the Java SDK and will be installed.

You already know what most of this code does apart from the IOException at the end. What this does is tell the Java system that something could go wrong in regards to I/O and to ready itself for such a situation. The main thing to remember is if using java.io.* is to ensure you include a throws IOException in your code.

This is an important part of the program.It creates a new BufferedReader object called in and tells it to read data from the standard input, in this case (and most others) the keyboard. Again don’t worry too much about the syntax just remember how to use it at this stage.

This is another key piece of the program and also a key piece of java. This line creates a variable, a String variable to be more precise. A variable is a place in your computers memory where you can store surprisingly enough variable information, or in other words information that can be changed. A String variable therefore can hold a String. A String is a line of text for example “the cat sat on the mat” is an example of a String variable. In this case it is called name and is set to initially hold nothing (or “”).

The first line shouldn’t be a problem. The second empties the buffer so that no garbage is  left in it to give you unexpected results.

This line is probably the MOST important in the program. Basically it reads from the keyboard using the readLine() method and stores the result in name (the String variable we created and set to “”). Notice how the readLine function is called. It belongs to the BufferedReader class of which ‘in’ is an instance we created above.

This line prints the results of the program to the screen, notice how the statement prints the contents of the name variable not “name” itself. If I had wanted to print “name” to the screen I would have put it between speechmarks. Therefore no speechmarks means ‘content of’ if you like.