The Hello World Program


Java is an object oriented programming language. We write classes and build objects out of them. A class is a blueprint or a representation of an object. It is an entity that doesn’t really exist while objects are those that exist in the memory of a computer. For example, a drawing of a car on a paper might be considered to be a class and cars built of them are objects. All objects built out of the same class are similar in the way that they possess the same set of properties but these properties have different values for each of the object just like a car having a colour which is different for each of the cars. And we have some methods as a part of the class which are used to perform specific tasks. And above all, we have constructors which are used to initialise the properties i.e. variables while creating an object. Programming in java therefore revolves around classes which consist of constructors, variables (also known as instance variables since each instances of the class has its own copy of these variables) and methods (also known as functions). A class may contain all of these three entities or one or more of them as per the requirement. We then write test classes in which we create objects from the classes. However, when we write simple programs as we would be doing now, we don’t think of programming in an object oriented way. We write a class, having a single method, ‘main’ which will be executed when the program is run.

Let’s get started with our first program in which we print a single line of text “Hello World” on the computer screen.

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

This class (or program) written by us has a single method named main and does not consist of any variables or constructors. The first line of the program declares the class which we have named HelloWorld. You may use any name according to what the program does. The naming of the programming serves as a means to identify the class and should reflect the purpose of the class. For example, a class written to represent a car should be named as ‘Car’ rather than as ‘Book’.

The class declaration starts with the word public. It is an access specifier about which we would look into later on. In addition to public, there are two other access specifiers, private and protected. The next word is ‘class’ which is used to declare a class. The next word ‘HelloWorld’ is a name for our class. We can use any name but it needs to follow the rules of naming identifiers. Class names, method names and variable names are known as identifiers. They serve the purpose of representing elements in our program. When those elements are required, we refer to them by their names.

Following are the rules to be followed while naming identifiers:

1. Identifiers may contain uppercase letters A-Z, lowercase letters a-z and the digits 0-9
2. The only allowed special characters are dollar $ and underscore _
3. An identifier should not begin with a number
4. Spaces should not be used
5. Keywords cannot be used as identifiers

Keywords are reserved words in a programming language. They have special meanings. The keywords which we have come across till now are ‘public’ and ‘class’.

Next, we see an open brace { and its corresponding closing brace } can be seen in the last line. Braces group things together. The braces here indicate that all of the code that appears within it is a part of the class HelloWorld.

Next we have the method declaration for the method ‘main’. Every program which needs to be run should have a main method. However, if a class is meant only to be used in other programs and has no need to be run, it need not have a main method.

Just like a class declaration, a method declaration also has several parts. The first is the keyword ‘public’, which is an access specifier. The next word ‘static’ is a keyword which is used to tell that this method is accessible without creating an object of this class. That is the reason why this particular method (main) is invoked directly when we run the program even without creating an object. The next keyword is ‘void’. Void states that this method does not return any value. We shall see more about it later on. The next word ‘main’ is an identifier. Just as we have named our class as HelloWorld, we have named our method as main. We can use any name for a method, but if it is the one to be invoked when the program is run, it should always be named as main with the specifiers public, static and void. Next we have a pair of parentheses in which ‘String args []’ is written. ‘String [] args’ can also be written as ‘String args []’. The square brackets can be placed either after the word String or the word args. These are known as parameters. We shall look into them later on. Here again, we have a pair of braces. These again indicate that the code that follows is a part of the method main.

In this method, we have a single statement. Note that all statements in Java are terminated by a semicolon. However, class and method declarations are not. This statement is used for printing on the screen. System is a predefined class just like our HelloWorld class. ‘out’ is a variable in that class which stores an object. Variables store information which can also be objects. And print is a method of that class just like the method main in this class. Note that a dot is used to refer to elements in a sequence just as a / separates the different parts in a URL. We have accessed the member out of the class System without creating an object. The reason is that out is defined as a static member. We shall see later on how objects are created. And to invoke a method, we follow the method name by parentheses and the required parameters are passed in it. Parameters are those values which are required by the method to perform its task. The method println () prints something on the screen but it needs to know what exactly it needs to print. This is passed as a parameter. Note that the sentence which we pass should be enclosed in double quotes. This is known as a String – a sequence of characters.

When we run this program, the text “Hello World” is displayed on the screen. Change “Hello World!” in the program to something else and it will be displayed on the screen.

So, in conclusion, when we wish to write a program, its structure should look in the following way:

public class <class name> {
   public static void main(String[] args) {
   <statements>
   }
}

Replace <class name> with the name that you wish to give to your program or class and replace statements with those required to perform the task. Everything else remains the same for simple and small programs. At this point of time, it would be helpful to know a few things. The first is that Java is a case sensitive language. Uppercase and lowercase letters are different. Change public in the first line of the program to Public and compile the program. You will be receiving compilation errors. There is no need to panic about remembering the case in which different parts of the program are to be written. All keywords are written in lower case and identifiers such as System (a predefined class) follow certain conventions which we shall see later on.

And another thing is that Java is a free form language. You can write your program in as many lines as you want. For example you could have written the first line of the program as

public
class
HelloWorld
{

The program still works! However, you can’t split single words. For example, you can’t write ‘cla’ on one line and ‘ss’ on the second line in the following way and expect the program to compile.

cla
ss

We can state a simple rule that in place of a single space, we can insert as many spaces or even a new line. And moreover, we can insert spaces and new lines before and after separators. We have the following separators in Java . , ; { } [ ] ( )

We could have written the System.out.println() statement as:

System
.
out    .
println    (
“Hello World!”;
)
;

However, here again “Hello World!” should be written on a single line. Strings in Java should begin and end on the same line.

Author: , 0000-00-00