Examining Hello WorldHello World is very close to the simplest program imaginable. Nonetheless there's quite a lot going on in it. Let's investigate it, line by line.For now the initial class statement may be thought of as defining the program name, in this case HelloWorld. The compiler actually got the name for the class file from the class HelloWorld statement in the source code, not from the name of the source code file. If there is more than one class in a file, then the Java compiler will store each one in a separate .class file. For reasons we'll see later it's advisable to give the source code file the same name as the main class in the file plus the .java extension. The initial class statement is actually quite a bit more than that since this "programcan be called not just from the command line but also by other parts of the same or different programs. We'll see more in the section on classes and methods below. The HelloWorld class contains one method , the main method. As in C the main method is where an application begins executing. The method is declared public meaning that the method can be called from anywhere. It is declared static meaning that all instances of this class share this one method. (If that last sentence was about as intelligible as Linear B, don't worry. We'll come back to it later.) It is declared void which means, as in C, that this method does not return a value. Finally we pass any command line arguments to the method in an array of Strings called args . In this simple program there aren't any command line arguments though. Finally when the main method is called it does exactly one thing: print "Hello Worldto the standard output, generally a terminal monitor or console window of some sort. This is accomplished by the System.out.println method. To be more precise this is accomplished by calling the println() method of the static out field belonging to the System class; but for now we'll just treat this as one method. One final note: unlike the printf function in C the System.out.println method does append a newline at the end of its output. There's no need to include a \n at the end of each string to break a line.
Exercises
Braces and BlocksLet's investigate the Hello World program a little more closely. In Java a source code file is broken up into parts separated by opening and closing braces, i.e. the { and } characters. Everything between { and } is a block and exists more or less independently of everything outside of the braces.Blocks are important both syntactically and logically. Without the braces the code wouldn't compile. The compiler would have trouble figuring out where one method or class ended and the next one began. Similarly it would be very difficult for someone else reading your code to understand what was going on. For that matter it would be very difficult for you, yourself to understand what was going on. The braces are used to group related statements together. In the broadest sense everything between matching braces is executed as one statement (though depending not necessarily everything inside the braces is executed every time). Blocks can be hierarchical. One block can contain one or more subsidiary blocks. In this case we have one outer block that defines the HelloWorld class. Within the HelloWorld block we have a method block called "main. In this tutorial we help to identify different blocks with indentation. Every time we enter a new block we indent our source code by two spaces. When we leave a block we deindent by two spaces. This is a common convention in many programming languages. However it is not part of the language. The code would produce identical output if we didn't indent it. In fact I'm sure you'll find a few examples here where I haven't followed convention precisely. Indentation makes the code easier to read and understand, but it does not change its meaning.
|
Bhopal news
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100