Saturday, 14 April 2012

Java Tutorial 3: The "Hello World!" Application

OK, we now know what JAVA is and have set-up the software environment for coding. Let’s write a simple program called "Hello World!"

First thing I want you to do is run eclipse, File,new,Java project and give it a name “HelloWorld”.  Then I want you to click the little arrow in front of the “HelloWorld” in the Package Explorer, right click on src, new, new class and name it  “HelloWorld again and delete everything in the text editor.

Then I want you copy the code bellow into the text editor. Don’t worry if you can’t understand everything. I’ll explain them latter. If you prefer to type it in, make sure you type exactly as below.

//The HelloWorldApp class implements an application that
//simply prints "Hello World!" to standard output.
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

Then save the file, click the green run arrow, select Java Application, you should be able to see "Hello World!" printed in the Console box.

Now that you've seen the "Hello World!" application, you might be wondering how it works. he "Hello World!" application consists of three primary components: source code comments, the HelloWorldApp class definition, and the main method. The following explanation will provide you with a basic understanding of the code, but the deeper implications will only become apparent after you've finished reading the rest of the tutorial, so don't worry if you cannot understand everything.

Source Code Comments


The following underlined text defines the comments of the "Hello World!" application:

//The HelloWorldApp class implements an application that
//simply prints "Hello World!" to standard output.
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

Comments are ignored by the compiler but are useful to other programmers. The Java programming language supports three kinds of comments, but for now, just remember the following one,
// text; the compiler ignores everything from // to the end of the line.

The HelloWorldApp Class Definition


The following underlined text begins the class definition block for the "Hello World!" application:

//The HelloWorldApp class implements an application that
//simply prints "Hello World!" to standard output.
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}


As shown above, the most basic form of a class definition is:
class name {
    . . .
}

The keyword class begins the class definition for a class named HelloWorldApp, and the code for each class appears between the opening and closing curly braces.

The main Method


The following underlined text begins the definition of the main method:

//The HelloWorldApp class implements an application that
//simply prints "Hello World!" to standard output.
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

In the Java programming language, every application must contain a main method whose signature is:
public static void main(String[] args)

The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above. You can name the argument anything you want, but most programmers choose "args" or "argv".

The main method accepts a single argument: an array of elements of type String. public static void main(String[] args)

Finally, the line:


System.out.println("Hello World!");



Uses the System class from the core library to print the "Hello World!" message to standard output. Portions of this library (also known as the "Application Programming Interface", or "API") will be discussed throughout the remainder of the tutorial.

1 comment:

  1. Thanks for your prompt guide. It's very useful for beginner like me.

    ReplyDelete