Java Reference
In-Depth Information
1 public class Hello2 {
2 public static void main(String[] args) {
3 System.out.println("Hello, world!");
4 System.out.println();
5 System.out.println("This program produces four");
6 System.out.println("lines of output.");
7 }
8 }
Notice that there are four semicolons in the main method, one at the end of each
of the four println statements. The statements are executed in the order in which
they appear, from first to last, so the Hello2 program produces the following output:
Hello, world!
This program produces four
lines of output.
Let's summarize the different levels we just looked at:
A Java program is stored in a class.
Within the class, there are methods. At a minimum, a complete program requires
a special method called main .
Inside a method like main , there is a series of statements, each of which repre-
sents a single command for the computer to execute.
It may seem odd to put the opening curly brace at the end of a line rather than on a
line by itself. Some people would use this style of indentation for the program instead:
1 public class Hello3
2 {
3 public static void main(String[] args)
4 {
5 System.out.println("Hello, world!");
6 }
7 }
Different people will make different choices about the placement of curly braces.
The style we use follows Sun's official Java coding conventions, but the other style has
its advocates too. Often people will passionately argue that one way is much better than
the other, but it's really a matter of personal taste because each choice has some advan-
tages and some disadvantages. Your instructor may require a particular style; if not, you
should choose a style that you are comfortable with and then use it consistently.
Now that you've seen an overview of the structure, let's examine some of the
details of Java programs.
 
Search WWH ::




Custom Search