Saturday, 12 December 2009

Java Introduction Part 1 - Hello World

As it is a common theme to introduce a language by providing a "Hello World" example, I have decided to follow the same pattern.

Below is a simple Java program which prints out in the console the message "Hello World". This simple program will print out in the console the message "Hello World !!!". Following the example, we will walk through each line and explain what it does. We will come across a lot of keywords and identifiers which will discuss in subsequent posts. For now we will just walk through the program to see how it works without worrying too much about the underlying complexity hidden in the aforementioned keywords and identifiers.

  1. // HelloWorld.java
  2. // Simple Java program that prints out in the console the message "Hello World !!!"

  3. public class HelloWorld
  4. {
  5.     // main method where the execution of a Java application begins
  6.     public static void main (String args[])
  7.     {
  8.         System.out.println("Hello World!!!");
  9.     } // end of main method
  10. } // end class HelloWorld

Let's take now each line and explain what the line does.

Lines 1-2:
  1. // HelloWorld.java
  2. // Simple Java program that prints out in the console the message "Hello World !!!"
As we can see both lines begin with two forward slashes ("//"). This symbol indicates that the remainder of the line is a comment. It is always a good practice to use comments in order to document programs and improve their readability. Inserting comments into a program also helps other people to read and understand programs. Comments are ignored by the Java compiler; therefore they do not affect the execution of the program when it runs.

Line 3: This is an empty line.

Line 4:
  
      4.  public class HelloWorld

This is a class declaration for class HelloWorld. Every program in Java should contain at least on class declaration which is defined by the programmer. The word class is a Java keyword and is used to declare a Java class, followed by the class name (HelloWorld in our example). Keywords (or reserved words) are special words in Java; they are always spelled in lower case and cannot be used by the programmers as identifiers.

* By convention all Java class names follow the pattern below:
  • Java class names begin with a capital letter
  • Capitalize the first letter of each subsequent word.
Example: SampleClassName.

Line 5:
  
      5.  {

Line 5 in this program is a left brace. This symbol begins the body of every class declaration. A corresponding right brace (at line 11) must end each class declaration.

Line 6:
  
      6.  // main method where the execution of a Java application begins

Line 6 (as lines 1 and 2) is an end-of-line comment. As we can see, from line 6 onwards, there is an indentation pattern which is one of the spacing conventions used in Java to make the programs more readable. This convention is used throughout the program and every time a left brace is opened, the following line is intended using the TAB button.

 
Line 7:
  
      7.  public static void main (String args[])

Line 7 is the starting point of every Java application. The parentheses after the identifier main indicate that this is a program building block called a method. Normally there are one or more methods defined within a Java class. Line 7 contains a number of keywords (marked in blue) which we will discuss now.

The keyword public is called an access modifier. This allows the class to be accessible from the entire workspace. Other access modifiers exist to limit the access of a class by other classes. More about access modifiers in subsequent posts.

The keyword static indicates that this method is special since it can be called without first creating an object of that class. More about objects in subsequent posts.

The keyword void indicates that this method will perform a task but will not return any information when that task is completed. More about return types in subsequent posts.


Line 8:
  
      8.  {

Line 8 is again a left brace. The body of the method declaration begins here. A corresponding right brace will end this method declaration's body in line 10. Again we can see that the following line is indented to the right using the TAB button.


Line 9:
  
      9.  System.out.println("Hello World!!!");

Line 9 instructs the computer to perform an action. It will print to the console the string of characters included within the double quotation marks. System.out is known as the standard output object and allows the Java application to display a set of characters in the command window where the Java application is run from. The System.out.println method prints a line of text in the command window. As with the main method, this method is followed by parentheses. Within the parentheses we specify the argument(s) of the method. In this example, we specify the string to be printed in the command window. The statement (action to be performed) ends with a semicolon (;) and the whole line 9 that ends with the semicolon is called a statement. Every statement must end with a semicolon.

* Please note that omitting the semicolon at the end of the statement is a syntax error and the program will not run.

Lines 10-11


      10.    } // end of main method
      11.  } // end class HelloWorld

Lines 10-11 contain the closing brackets for the method's declaration body and the class. They also contain comments to make the program more readable. 

This concludes Part 1 of the Java Introduction posts. We have written a simple Java program that prints "Hello World !!!" to the command window and we have discussed the various bits and bobs that make up the program.  

No comments:

Post a Comment