Home‎ > ‎Applied Core Java‎ > ‎Introduction‎ > ‎

A Closer look at the "FirstHello.java" program

I am very sure that you must have run your first java program "FirstHello" so far.  Are you wondering how it works?  Check below details:

/**

*/

public class FirstHello {
   public static void main(String[] args) {

          System.out.println("Hello Java");
   }

}

The "FirstHello" program consists of three primary components: source code comments, the FirstHello class definition, and the main method. The following explanation will provide you with a basic understanding of the code, but the deeper implications will only become apparent after you've finished reading the rest of the tutorial.

Source Code Comments


The following color text defines the comments of the "FirstHello" program:

public class FirstHello {
public static void main(String[] args) {

System.out.println("Hello Java");

}
}

Comments are ignored by the compiler but are useful to other programmers. The Java programming language supports three kinds of comments:

/* text */

The compiler ignores everything from /* to */.

/** documentation */
This indicates a documentation comment (doc comment, for short). The compiler ignores this kind of comment, just like it ignores comments that use /* and */. The javadoc tool uses doc comments when preparing automatically generated documentation. For more information on javadoc, see the JavadocTM tool documentation .

// text
The compiler ignores everything from // to the end of the line.


Class Definition


The following color text begins the class definition block for the "FirstHello!" program:

public class FirstHello {
public static void main(String[] args) {
 System.out.println("Hello Java");

}
}

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

public class name {
. . .
}

public is a Java keyword which declares a member's access as public ,the keyword class begins the class definition for a class named name, and the code for each class appears between the opening and closing curly braces marked in color above. For now it is enough to know that every program begins with a class definition.

The main Method


The following color text begins the definition of the main method:

public class FirstHello {
public static void main(String[] args) {
 System.out.println("Hello Java");
}

}

In the Java programming language, every program must contain a main method whose signature is:
 public static void main(String[] args)

The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above. You can name the argument anything you want, but most programmers choose "args" or "argv".

The main method is similar to the main function in C and C++; it's the entry point for your program and will subsequently invoke all the other methods required by your program.

The main method accepts a single argument: an array of elements of type String.
 public static void main(String[] args)

This array is the mechanism through which the runtime system passes information to your program. Each string in the array is called a command-line argument. Command-line arguments let users affect the operation of the program without recompiling it. For example, a sorting program might allow the user to specify that the data be sorted in descending order with this command-line argument: -descending

The "FirstHello" program ignores its command-line arguments, but you should be aware of the fact that such arguments do exist.

Finally, the line: System.out.println("Hello Java");
uses the System class from the core library to print the "Hello Java" message to standard output. Portions of this library (also known as the "Application Programming Interface", or "API").


<<Previous | Next>>
Comments