A Simple HelloWorld.java program


Let's look into our first program. Save the following text into the file called HelloWorld.java


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


We examine that there is a class called HelloWorld. Since HelloWorld is the only class in the file, it is called the main class. The main class of the file has to be runnable in order to make your program runnable. In order to make your program runnable, you should have a function called main which is defined as:

     public static void main(Strings[] args)

The explanation is as follows:

The public qualifier tells you that the function main is callable from outside. You don't have to worry about this one yet as the concept of making a function private or public is later described in Object Oriented Programming (OOP) philosophy.

Then the next qualifier is static. You don't have to worry about this either.
Then, void tells you that the function main returns nothing to the system.
Then the function's name, main.
Then String[] args denotes the argument passed into the program. The arguments are not used in the moment. What is an argument? It is the options or switches you would mention when invoking programs. Like in dir /w or ls -la both /w and -la serve as arguments.

You can consider System.out.println as a command to print some text on the screen. Not that the text has to be surrounded by the double quotes. In this case, "Hello World!" is being printed, without the quotes of course. Remember that almost every command in Java ends with a semicolon(;).

IMPORTANT: The main class name has to be the same as the program file name. Otherwise, your Java program won't compile. In this example the class name is HelloWorld. The program file has to be HelloWorld.java. You have to pay attention that this naming convention is CASE SENSITIVE it means that naming your file into helloworld.java WON'T work. If your program won't compile, pay attention to the capitalization of your file name. This rule also applies even in Windows platform.

How to Compile and Run Your Program
In either Windows or UNIX, to compile the program, type the following command at your prompt: Don't forget to change the directory to the directory where your Java program resides. Also, make sure that your JDK installation is correct.

javac HelloWorld.java
In Macintosh, just drag your HelloWorld.java file into the Java Compiler icon.

If it produces a file called HelloWorld.class, it means that your compilation is successful.

To run it, under Windows or UNIX, type the following:

java HelloWorld
Notice that there is no .class extension on it.

Under Macintosh, simply double-click your HelloWorld.class file.

Commenting Your Program
Sometimes, it is useful to make some note about your program. Not only making you clear about what's going on your program, but also making others able to read your program based on the comments of your program. The compiler will completely ignore your comment, however. You cannot persuade the compiler to do your program eventhough your program has errors. In Java, there are three ways of commenting:

Double slash comment. Example:
// Put your comments here
After you put the double slash (//), everything else behind it is considered as comment.
Embracing comment. Example:
        My comment is here
        Neat huh?
If your comment is pretty long, it is better to embrace it with /* and */. Everything within it is considered as a comment.
Java doc comment. It is similar to above, except:
       My comments here will be put into
       documentation by Javadoc program
It is embraced with /** and */. Notice the double star. Your comments within it will be documented by the Javadoc program, an automatic documentation maker from Java SDK package.

Well Done!
Well done! You have completed the first Java Program.

Quick Links:
Have Java Problem
Do you have a Java Question?

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Best Regards,
Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.