chapter 2 how to compile and execute a simple program

32
Chapter 2 How to Compile and Execute a Simple Program

Upload: marian-joseph

Post on 26-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 2 How to Compile and Execute a Simple Program

Chapter 2

How to Compile andExecute a Simple Program

Page 2: Chapter 2 How to Compile and Execute a Simple Program

• 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.

Page 3: Chapter 2 How to Compile and Execute a Simple Program

• 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.

Page 4: Chapter 2 How to Compile and Execute a Simple Program

• 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 | ---------------------------* *---------------------------*

Page 5: Chapter 2 How to Compile and Execute a Simple Program

• 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.

Page 6: Chapter 2 How to Compile and Execute a Simple Program

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

Page 7: Chapter 2 How to Compile and Execute a Simple Program

• 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.

Page 8: Chapter 2 How to Compile and Execute a Simple Program

• 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

Page 9: Chapter 2 How to Compile and Execute a Simple Program

• 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; }

Page 10: Chapter 2 How to Compile and Execute a Simple Program

• 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.

Page 11: Chapter 2 How to Compile and Execute a Simple Program

• 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; }

Page 12: Chapter 2 How to Compile and Execute a Simple Program

• 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 ex

pression, 6 + 9 + 8, and the statement terminator, a semicolon, ;.

Page 13: Chapter 2 How to Compile and Execute a Simple Program

• 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.

Page 14: Chapter 2 How to Compile and Execute a Simple Program

• 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.

Page 15: Chapter 2 How to Compile and Execute a Simple Program

• 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.

Page 16: Chapter 2 How to Compile and Execute a Simple Program

• 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; } }

Page 17: Chapter 2 How to Compile and Execute a Simple Program

• The semicolon, ;, the parentheses, (), and the braces, {}, act as punctuation.

• Occasionally, such markers, in such contexts, are called punctuators.

Page 18: Chapter 2 How to Compile and Execute a Simple Program

• 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.

Page 19: Chapter 2 How to Compile and Execute a Simple Program

• 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); } }

Page 20: Chapter 2 How to Compile and Execute a Simple Program

• 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.

Page 21: Chapter 2 How to Compile and Execute a Simple Program

• 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 ");

Page 22: Chapter 2 How to Compile and Execute a Simple Program

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

Page 23: Chapter 2 How to Compile and Execute a Simple Program

• 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 ");

Page 24: Chapter 2 How to Compile and Execute a Simple Program

• 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.

Page 25: Chapter 2 How to Compile and Execute a Simple Program

• 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.

Page 26: Chapter 2 How to Compile and Execute a Simple Program

• 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.

Page 27: Chapter 2 How to Compile and Execute a Simple Program

• 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);

}

}

Page 28: Chapter 2 How to Compile and Execute a Simple Program

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

Page 29: Chapter 2 How to Compile and Execute a Simple Program

• 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.

Page 30: Chapter 2 How to Compile and Execute a Simple Program

• 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.

Page 31: Chapter 2 How to Compile and Execute a Simple Program

• 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

Page 32: Chapter 2 How to Compile and Execute a Simple Program

• 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.