Ye Olde Hello World!





Welcome

Hi! Welcome to our first lesson. Here we will meet our first Java program: The Big Old Famous Hello World. Well, I think that's a very good approach in programming. This example is widely used since... 1970 I guess... when Ritchie & Kernighan first introduced C. That's why I called that "the big old famous". After following this chapter, I hope that you can know how to compile your Java program and how to display a text on screen. I won't discuss about the installation of JDK or your GUI, because it goes beyond the scope. I understand that the operating system platform will be very much different. I'll be using Windows. Eventhough, Linux and Unix variant will be much similar, I suggest that you consult the manual. Mac or other platform will be completely different, but I think that's pretty simple. Consult the manual, too. You'd probably want to click here on official Sun's Java document in compiling Java programs on various platform.

NOTE: When I mention about Windows, I mean Windows 3.x/9x/NT/2000 altogether and when I mention UNIX, all *NIX variant is included (i.e. Linux, Solaris, etc.).

 

HelloWorld.java

Let's look into our first program. Save the following text into the file called HelloWorld.java


class HelloWorld {
     public static void main(String[] args)
     {
           System.out.println("Hello World!");
     }
}

We examine that there is a class called HelloWorld. Since HelloWorld is the only class in the file, it is called the main class. The main class of the file has to be runnable in order to make your program runnable. In order to make your program runnable, you should have a function called main which is defined as:

     public static void main(Strings[] args)

The explanation is as follows:

You can consider System.out.println as a command to print some text on the screen. Not that the text has to be surrounded by the double quotes. In this case, "Hello World!" is being printed, without the quotes of course. Remember that almost every command in Java ends with a semicolon(;).

IMPORTANT: The main class name has to be the same as the program file name. Otherwise, your Java program won't compile. In this example the class name is HelloWorld. The program file has to be HelloWorld.java. You have to pay attention that this naming convention is CASE SENSITIVE it means that naming your file into helloworld.java WON'T work. If your program won't compile, pay attention to the capitalization of your file name. This rule also applies even in Windows platform.

 

How to Compile and Run Your Program

In either Windows or UNIX, to compile the program, type the following command at your prompt: Don't forget to change the directory to the directory where your Java program resides. Also, make sure that your JDK installation is correct.

javac HelloWorld.java

In Macintosh, just drag your HelloWorld.java file into the Java Compiler icon.

If it produces a file called HelloWorld.class, it means that your compilation is successful.

To run it, under Windows or UNIX, type the following:

java HelloWorld

Notice that there is no .class extension on it.

Under Macintosh, simply double-click your HelloWorld.class file.

 

Commenting Your Program

Sometimes, it is useful to make some note about your program. Not only making you clear about what's going on your program, but also making others able to read your program based on the comments of your program. The compiler will completely ignore your comment, however. You cannot persuade the compiler to do your program eventhough your program has errors. In Java, there are three ways of commenting:

  1. Double slash comment. Example:
    // Put your comments here
    After you put the double slash (//), everything else behind it is considered as comment.
  2. Embracing comment. Example:
    /*
            My comment is here
            Neat huh?
    */
    If your comment is pretty long, it is better to embrace it with /* and */. Everything within it is considered as a comment.
  3. Java doc comment. It is similar to above, except:
    /**
           My comments here will be put into
           documentation by Javadoc program
    */
    It is embraced with /** and */. Notice the double star. Your comments within it will be documented by the Javadoc program, an automatic documentation maker from Java SDK package.

 

Well Done!

Well done! You have completed the first chapter of Java lesson 1. Move on to the next chapter.

 


Where to go

Chapter 2
News Page
Lesson 1 contents
Contacting Me


Roby Joehanes © 2000

1