[Português]
 [Home]
 [Back]
 [Help]

Ricardo's Home Page

Java Application HelloWorld


 Java
 Links
 Programs
 Info
 Guestbook

The Hello World application should print in you screen the message "Hello World" each time it is run.
Ok, this is not the most useful application one can write, but I'll try to use it to explain the basis of a Java application.
Please take a look at the code:


1:
2:
3:
4:
5:
6:
7:

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

The first line has the statement class HelloWorld.
Any valid java application must have, at least one class defined.
The class qualifier precedes a class definition.
The word HelloWorld is the name we choose for this class.
Please notice that this is the same name we choose for the file.

In line 2 we can see a opening curly bracket, witch pairs with the closing one in line 7.
The same applies to the curly brackets in lines 4 and 6.
A pair of matching curly brackets defines a block of code that is associated with the definition that precedes it.
This way, the first curly bracket pair (lines 2 and 7) defines the body of the class HelloWorld. The second one, (lines 4 and 6) defines de body of the function main.

The line 3 has the statement public static void main(String[] arg).
This is the definition of the first method (function) of the class HelloWorld.
In reality this is the only method we need for now.
Any valid java application must have a main method defined in the primary class of the application (HelloWorld).
The main method name is preceded by the public static void statements and followed by (String[] arg) definition.
The first statements define the properties of the method, and the second definition identifies main as a function and defines it's arguments.
The public statement tell the compiler that this method (main) should be accessible by any program that can access this class (HelloWorld).
This is necessary because, when we try to run this application the java runtime will try to access the main method, so it should be accessible to the outside (public).
The static statement tells the compiler this method can be run directly without a class instantiation. This is an imposition of the java language.
The void statement tells the compiler that the method is not returning any value to the calling system.
After the name of the method (main) we can see a pair of matching parentesis.
They are mandatory in any method definition following the name of the method.
Inside of the parenthesis we can write a parameters list for that method.
Although we are not using any parameters, we must define an array of strings as the only parameter of the main method.
This is an imposition of the Java language. The main method that must be defined in any java application must have a string list as parameter.

In line 4 we can see another opening curly bracket, matching with the one in line 6.
This pair defines the body of the main method. Only one line.

Finally, in line 5 we have our program.
A very short program as it has only one line of code (System.out.println("Hello World !!!");).
The first part ( System.out.println() ) tells the compiler the java language method we want to use to print our phrase.
The argument we pass to it ("Hello World !!!") is what we want to be printed.
Please notice the semi colon at the end of the statement.
In java, as in C or C++ language, each statement must end with a semi colon.
Notice that method or class definitions and code blocks (pairs of matching curly brackets) don't need a semi colon at the end.

Now we have our application written.
We need to save it to a file with the same name of the class that defines the main method with an extension ".java".
Our file will be HelloWorld.java
Please notice that java is case sensitive, so you need to respect the capitalization.
Notice also that the line numbers in the above example should not be inserted in the file and are used only for the improve readability in our example.

Now we need to compile and run our example.
If you have not done so you must begin by downloading and installing the JDK.
Now we need to compile our application.
We can do it by calling the java compiler as follows.


D:\Example\HelloWorld>javac HelloWorld.java

If you have difficulties in finding the javac compiler you may need to add the complete path to the compiler (c:\jdk1.2\bin\javac for example) or add it to your environment.
Now you must have a new file named HelloWorld.class in the same directory of the source file HelloWorld.java.
This is our application.

To run our new application we should invoque the java virtual machine.
We do that by calling the java program.


D:\Example\HelloWorld>java HelloWorld
Hello World !!!

You may notice that we omitted the file extension.
In fact java will try to find the application in the places we defined during the installation.

That's it for now.
You should try to mess around with the example presented.
Try to change the sentence the application outputs, try to repeat line 5 several time with diferent sentences, etc...
You may want to compare this example with the same example mounted as a servlet or as a applet and see the diferences between those implementations.


Last update:
2000/01/08
This page hosted by
Get your own Free Homepage
Prepared by:
Ricardo Lindengrün

Click Here!