Java programs consist of modules called classes. Each class contains methods that perform tasks and return information when they have finished. Like most languages there is a large collection of pre-defined classes and methods in what is called the Java Class Library (also known as the Java API).
By using Java API classes and methods instead of writing your own will improve your java program's portability as these methods and classes are included in every java implementation.
Besides, who wants to reinvent the wheel? And it's always good not to try and manipulate the language in a strange way. Troubleshooting and debugging will be simpler if you write your Java code in a straightforward manner.
So let's look at HelloWorld.java
CODE
/* Program name HelloWorld.java
who's sole purpose is
to
introduce java programming */
public class HelloWorld{
public static void main (String args[])
{
System.out.println("Hello World"); // note the semi-colon after the statement
} // end of the main method
} // end of Hello World
You will notice the multi-line comment at the very beginning of the code. Comments can be written 3 ways. A single line comment is denoted by a //, where as a multi-line comment begins with /* and ends with */. All text between /* and */ are ignored by the compiler. The third and final format for a comment is a documentation comment which is delimited by /** */. If you forget to close off the end of a multi-line comment or close it incorrectly, it will appear as a syntax error to the compiler.
If you are familiar with C or C++ you will notice the similarities between their comments and Java€™s. Only the document comment is special to Java. It was designed to allow programmers to embed documentation for their programs in the programs themselves. The javadoc utility program (in the Java2 SDK) reads those comments and uses them to prepare the program documentation.
Line 4 of the code;
CODE
public class HelloWorld{
This is the beginning of a class definition for a class called HelloWorld. Every Java program has at least one class definition, because a class is basically a java program. The class keyword is immediately followed by the class name. HelloWorld is the class name in the above definition. The HelloWorld class must be stored in a file called HelloWorld.java or it will not be compiled correctly.
Most often class names begin with a capital letter; it makes it much simpler to identify class names if you follow this naming convention. It is important to note that Java is case sensitive so helloWorld and HelloWorld are 2 different class names. Also, class names can contain underscores ( _ ) and dollar signs ( $ ), but may not begin with a digit ( 0-9 ) nor contain spaces. I would recommend avoiding using $ as a class identifier, because the compiler often uses dollar signs to create identifier names.
Now the keyword public is a member access modifier. It determines if other classes and methods have access to the java application. A member with a public access modifier is accessible wherever the program has a reference to HelloWorld. You can only ever have one public class in a Java file. Defining more than one public class will result in a syntax error.
It has always been my belief that when you open a bracket { you should immediately close it } and then program within them. This way you will never run into the problem of unclosed methods or classes. If you are missing a bracket the compiler will report an error.
The next line is important as well:
CODE
public static void main (String args[])
This is the core of the Java program. This is the main method within the HelloWorld class. It is the first thing executed within the program. If there is no main defined as shown the java interpreter will not execute the application. And you will receive an error similar to this:
CODE
In class NoMain: void main(String argv[]) is not defined
In order to begin the content of this method you must have the { } to contain it.
Again the public modifier shows that the main method can be called by any object. The static modifier on the other hand states that the information stored is class wide information. In other words, all objects within the class share the same piece of data.
The void keyword tells you that this method will perform whatever task it is given but not return any values. There are many different methods of returning information that I'll discuss in other tutorials.
Now:
CODE
System.out.println("Hello World"); // note the semi-colon after the statement
System.out is known as the standard output object. It allows the Java application to display strings and other types of information in the command window from which the application executes. So this line above will print Hello World in the command window. Once println is complete it will put the cursor on the next line. Similar to when you hit the enter key.
You will notice that this line of code is the only one in the program to end with a semicolon ( ; ). The semicolon indicates the end of a statement. Every statement in Java must end in a semicolon.
Now to compile and execute the code you will need at least 2 items from the Java SDK. First, and foremost, the javac.exe which is the java compiler program. It will turn the source code above into files that can be read and understood by the java interpreter.
Next is the java.exe which is the java interpreter. It will read the files created by the java compiler and execute the code therein.
Once you are done your code to compile the program you would use the following in the command prompt:
CODE
javac HelloWorld.java
If the compiler runs into no errors in the code, the preceding command creates a new file called HelloWorld.class. This file will then be interpreted by the java interpreter when it is told to execute the file.
To execute the file type
CODE
java HelloWorld
And that's it!