Java Reference
In-Depth Information
The instructions or statements in the body of the main methodȌthat is, the statements
inside the curly braces ( {} )Ȍare executed one by one. Each statement ends in a
semicolon ( ; ). Our method has a single statement:
System.out.println("Hello, World!");
This statement prints a line of text, namely ȒHello, World!ȓ. However, there are many
places where a program can send that string: to a window, to a file, or to a networked
computer on the other side of the world. You need to specify that the destination for
the string is the system outputȌthat is, a console window. The console window is
represented in Java by an object called out . Just as you needed to place the main
method in a HelloPrinter class, the designers of the Java library needed to place
the out object into a class. They placed it in the System class, which contains useful
objects and methods to access system resources. To use the out object in the System
class, you must refer to it as System.out .
To use an object, such as System.out , you specify what you want to do to it. In this
case, you want to print a line of text. The println method carries out this task.
You do not have to implement this methodȌthe programmers who wrote the Java
library already did that for usȌbut you do need to call the method.
Whenever you call a method in Java, you need to specify three items (see Figure 12 ):
A method is called by specifying an object, the method name, and the method
parameters.
1. The object that you want to use (in this case, System.out )
2. The name of the method you want to use (in this case, println )
3. A pair of parentheses, containing any other information the method needs (in
this case, ÑHello, World!Ñ ). The technical term for this information is a
parameter for the method. Note that the two periods in
System.out.println have different meanings. The first period means
Ȓlocate the out object in the System classȓ. The second period means Ȓapply
the println method to that objectȓ.
20
Search WWH ::




Custom Search