Sams Teach Yourself Java™ in 24 Hours (Covering Java 7 and Android)

(singke) #1
ptg7068951

40 HOUR 4:Understanding How Java Programs Work


When you run a Java application, the Java Virtual Machine (JVM) looks for a
main()block and starts handling Java statements within that block. If your
program does not have a main()block, the JVM responds with an error.

LISTING 4.1 The Full Text of Root.java
1: classRoot {
2: public static voidmain(String[] arguments) {
3: int number = 225;
4: System.out.println(“The square root of “
5: + number
6: + “ is “
7: + Math.sqrt(number)
8: );
9: }
10: }

The Rootapplication accomplishes the following tasks:

. Line 3: An integer value of 225 is stored in a variable named number.
. Lines 4–8: This integer and its square root are displayed. The
Math.sqrt(number)statement in Line 7 displays the square root.


If you have entered Listing 4.1 without any typos, including all punctua-
tion and every word capitalized as shown, you can run the file in NetBeans
by choosing Run, Run File. The output of the program appears in the out-
put pane, as shown in Figure 4.1.

Output Pane

FIGURE 4.1
The output of the Rootapplication.

Free download pdf