Dissection of the “Hello World” Application
First we start with the comments as HelloWorldAppc.java starts with the comments. The Java 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 JDK javadoc tool uses doc comments when preparing automatically generated documentation.
// text
The compiler ignores everything from // to the end of the line.
Now lets look at the class definition, in the Java language the simplest form of a class definition is
class MyClass{
.
.
}The keyword class begins the class definition for a class named MyName. The variables and methods of the class are enclosed by the curly brackets that begin and end the class definition block. The "Hello World" application has no variables and has a single method named main.
Every Java application must contain a main method whose signature looks like this:
public static void main(String[] args)
The method signature for the main method contains three modifiers:
public indicates that the main method can be called by any object.
static indicates that main method is a class method.
void indicates that the main method doesn't return any value.
All this will be explained in detail later in the course so don’t worry much about it now.
The main method in the Java language is similar to the main function in C and C++. When the Java interpreter executes an application, it starts by calling the class's main method. The main method then calls all the other methods required to run your application.
If you try to invoke the Java interpreter on a class that does not have a main method, the interpreter refuses to compile your program and displays an error message similar to this:
In class NoMain: void main(String argv[ ]) is not defined
As you can see from the following code snippet, 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 application. Each String in the array is called a command-line argument. Command-line arguments let users affect the operation of the application without recompiling it.
The program has one line of code which performs an action. This action is to print the string of text “Hello World!” to the screen. This code is
System.out.println(“Hello World!”);
System is a class which is available by default. It has a method called println that prints the string and appends a carriage return. The semicolon at the end terminates the line.
No comments:
Post a Comment