Java TUTORIAL for beginners

You have made prgress in running the Hello World program. It is now time to understand the code. Let us take a look at the code again,

class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Prints Hello world

There are basically three components in this program. And we have already understood first one - the java comment. The rest two are the HelloWorldApp class definition and the main method.

The Class method

Every application begins with a class. So every Java program will have at least one Class. A class is defined as follows

As shown above, the most basic form of a class definition is:

class AnyClassName {
    . . .

A class definition starts with the keyword class followed by the class name. In our example we used HelloWorldApp as class name, but we could use any name of our choice. We generally select the class name that bears similarity to the function it performs. A class name starts with a curly bracket followed by one or more java statements and functions and then ends with a closing curly braket.

The main method

Every application must contain a main method which looks like

public static void main(String[] args)

The main method is similar to the main function in C and is the first statement to be executed. The main method tajes only one argumeny which is an array of strings. What is the use of this argument ? If you recall our example we run the program as

java HelloWorld

And it prints "Hello World". What if you wish to say - Hello World X, where X is a variable passed during the invocation of the java HelloWorld. We could write something like

java HelloWorld X

and use X as a variable argument. This X is essentially one of the args array elements. In general we could invoke the Java program like

java MyApp arg1 arg2

and pass the two arguments arg1 and arg2 to the main function. In the Hello World example we did not use any argument, but a most general Java program may use it.

The main function has public and static as modifires that can be written in any order. Since the main method does not return anything we have void keyword just preceding main.

Finally, the line:

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

uses the System class from the core library to print out the "Hello World!" on the console.We will use it frequently in the coming tutorials.