Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2 How to Compile and Execute a Simple Program.

Similar presentations


Presentation on theme: "Chapter 2 How to Compile and Execute a Simple Program."— Presentation transcript:

1 Chapter 2 How to Compile and Execute a Simple Program

2 When you work with Java, you work either within a vendor-specific development environment or within a general-purpose editor, with which you write your program, and a compiler, with which you translate your program into a form with which your Java virtual machine can work. If you try to learn Java using a vendor-specific development environment, you learn a great deal about the vendor-specific development environment, but not enough about Java itself. Accordingly, this book introduces Java in the expectation that you will use a traditional editor and compiler.

3 In its original form, your program is text or source code. Once translated, the source code becomes byte code. You use the Java virtual machine to execute your program, or, said another way, you run your program.

4 You generally go around two key loops many times as you search for bugs. *---------------------------* | Write or edit source code | <--* <---------------------* *---------------------------* | | | | Fix bugs that | v | emerge during | *---------------------------* | compilation | | Compile source code | ---* | *---------------------------* | | Fix bugs that | v emerge during | *---------------------------* execution | | Execute program | ---------------------------* *---------------------------*

5 All Java programs contain one or more class definitions, each of which may contain various method definitions. In particular, every standalone Java program must contain a class definition that defines a method named main. When you start a Java program, the Java virtual machine performs the computations specified in the main method, thereby executing your program.

6 Applets developed exclusively for use in web browsers do not contain a main method, because they are not standalone programs.

7 Java's methods take the place of the functions or procedures that programmers talk about when they work with other languages. Methods are much like functions and procedures, except that each method definition must be embedded in a class definition.

8 Suppose, for example, that you want to compute the overall rating of a movie, given integers that specify individual ratings for the script, acting, and direction. For the moment, assume each individual rating contributes equally to the total rating. In Java, your program will contain a simple summing expression: 6 + 9 + 8

9 To arrange for your arithmetic expression to be evaluated, you define main, inside a class definition, such that the arithmetic expression appears in that method. public static void main (String argv[]) { 6 + 9 + 8; }

10 Keywords are words to which Java attributes special meanings. Three such keywords appear in the example. *-- Keywords | v ------------------ public static void main (String argv[]) { 6 + 9 + 8; } –The keyword public indicates how accessible the main method is to be. –The keyword static indicates that the main method is a class method, rather than an instance method. –The keyword void indicates that the main method returns no value.

11 Following the method name, main, you see a parameter specification surrounded by parentheses. *-- Parameter specification | v ------------- public static void main (String argv[]) { 6 + 9 + 8; }

12 Finally, you come to the method's body. In general, a method's body consists of matched braces surrounding a sequence of one or more statements, which tell the Java compiler what computations to perform. In the example, the body exhibits only one statement: public static void main (String argv[]) { 6 + 9 + 8; <-- Body statement } The statement, like most Java statements, consists of an expression, 6 + 9 + 8, and the statement terminator, a semicolon, ;.

13 At this point, you are ready to embed the main method in a class definition. Because you are defining a demonstration program, you name the class Demonstrate. You must store the definition of the Demonstrate class in a source file. The source file's file name must be Demonstrate, the name of the class contained in the file, and the source file's extension must be java.

14 Most Java programmers, by convention, start each class name with an uppercase letter. You should adhere to this convention; otherwise, other programmers may have difficulty understanding your work.

15 The definition of the Demonstrate class begins with two keywords: public and class. *-- Keywords | v ------------ public class Demonstrate {... } –The keyword public indicates how accessible the Demonstrate class is to be. –The keyword class indicates that a class is about to be defined.

16 Following the name of the class, Demonstrate, you come to the body of the class definition. In the example, the body contains a single method definition—the one for the main method—which computes the rating of a movie, given ratings for the movie's script, acting, and direction. public class Demonstrate { public static void main (String argv[]) { 6 + 9 + 8; } }

17 The semicolon, ;, the parentheses, (), and the braces, {}, act as punctuation. Occasionally, such markers, in such contexts, are called punctuators.

18 Note that the sample program is catatonic: It accepts no input data and produces no output result. And, because the program does nothing with the arithmetic it performs, discriminating Java compilers refuse to compile it.

19 To relieve programs of their catatonia, you can include display statements that tell the Java compiler that you want information to be displayed, as in the following revised program, which, when executed, displays The rating of the movie is 23 : public class Demonstrate { public static void main (String argv[]) { System.out.print("The rating of the movie is "); System.out.println(6 + 9 + 8); } }

20 The display methods, print and println, display information delimited by parentheses. Whenever you use println instead of print, Java not only displays information, but also terminates the line on which the information is displayed.

21 In Java, whenever quotation marks delimit a sequence of characters, those characters are the contents of a string. In the sample program, there are two display statements, the first of which displays a string. *-- String | v ----------------------------- System.out.print("The rating of the movie is ");

22 In the second instance, the display method displays the result produced by an arithmetic expression: *-- Arithmetic expression | v --------- System.out.println(6 + 9 + 8);

23 The System.out part of the display statement stipulates that the information is to be shown on your computer's display: *-- Stipulates where information is to be sent | v ----------- System.out.print("The rating of the movie is ");

24 Said with more precision, print and println are display methods that are defined to work on instances of the PrintStream class. System.out is an expression that produces the particular instance of the PrintStream class associated with your display.

25 A value that appears explicitly in a program is said to be a literal. –Explicit numbers, such as 6, are integer literals. –Explicit strings, such as "The rating of the movie is ", are string literals.

26 Spaces, tabs, line feeds, and carriage returns are said to be whitespace characters. Java is blank insensitive: It treats all sequences of whitespace characters—other than those in strings—as though there were just a single space.

27 Thus, the following are equivalent: public class Demonstrate { public static void main (String argv[]) { System.out.print("The rating of the movie is "); System.out.println(6 + 9 + 8); } } public class Demonstrate { public static void main (String argv[]) { System.out.print("The rating of the movie is "); System.out.println(6 + 9 + 8); } }

28 Java is case sensitive; if you write Main or MAIN when you mean main, Java cannot understand your intent.

29 At this point, you have seen sample uses of just one Java operator: the addition operator. In general, an operator is a built-in method that works on inputs supplied to it according to the conventions of arithmetic; such methods are interspersed among their inputs, and those inputs are called operands.

30 To initiate compilation on a Windows system, you open a window. Next, you type the following, assuming that your class definition is in a source file named Demonstrate.java : javac Demonstrate.java Such a line is called a command line. The example command line has two parts: *-- Name of the Java compiler | | *-- Source file's name and extension | v v ---------------- javac Demonstrate.java The Java compiler places the resulting byte code in a file named Demonstrate.class.

31 Once the Java compiler has placed the resulting byte code in Demonstrate.class, you can execute the main program defined inside the class definition by typing another command line: *-- Name of the Java virtual machine | | *-- Object file's name | v v ----------- java Demonstrate

32 Although the sample program communicates with you by way of output data displayed on your screen, it does not receive any input data after it has been compiled. Instead, it works with data that were supplied as the program was written. Such data are said to be wired in or hard coded.


Download ppt "Chapter 2 How to Compile and Execute a Simple Program."

Similar presentations


Ads by Google