1.1.2 What Does the Java Code of the Echo Application Mean?


Let us try to understand the Java code of the Echo application:
class Echo {
  public static void main (String[] args) {
    System.out.println(args[0]);
    System.out.println(args[0]);
  }
}
Line-by-line the code reads as follows:

The class keyword
In the Java language, all data and methods to operate on the data are collected within classes. Here we have one class defined, viz. Echo. It is good practice and often obligatory to let the name of the source file (Echo.java) correspond with the name of the main class (Echo) defined. Also use separate files for separate classes.

The main method
Our program contains only one method, viz. main. It refers to the main execution loop and is invoked by the Java interpreter at start-up.

The keywords public, static, and void
What is important to know right now is that every Java application must contain the main method that looks like:
public static void main(String[] args) {
  ...
}
The keyword public indicates that the method can be called by any object. The keyword static turns main in what is called a class method, which does not need an object to be invoked. void is the type of the value returned by the method; in this case, no value is returned. These keywords will be discussed later in more detail.

String[] args
The definition of the main method has one parameter: an array of strings. This array will enable your program to operate on actual arguments passed to application in the command-line.

System.out.println
System.out.println prints out its argument, which in this case is the first of the command-line arguments, on standard output (mostly your terminal screen).

Punctuation
You may write a statement in more than one line and you may freely use blanks to increase readability of code, but

in Java, every statement must be terminated by a semicolon.

In Java, the period (single dot) is used to refer to variables and methods of classes. For example, you can read System.out.println as follows: along with the Java system comes the class System that contains a variable out of type PrintStream; its full name is java.lang.System.out. This object or instance implements the standard output stream. On this object we apply the println belonging to the PrintStream class. Soon you will see easier examples of the use of periods.

Brackets
In Java, brackets are used for the following purposes:
Round brackets ()
Arguments of methods are placed between round brackets. Furthermore, round brackets are used in mathematical formulas to specify priorities of operations, and in control structures such as for-loops and if-clauses.
Square brackets []
Square brackets are used for declaration of arrays and for selection of array elements.
Curly brackets {}
Curly brackets are used for collecting statements in blocks. For example, the definition of a class and of a method is placed between curly brackets.