Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2 JAVA FUNDAMENTALS

Similar presentations


Presentation on theme: "Chapter 2 JAVA FUNDAMENTALS"— Presentation transcript:

1 Chapter 2 JAVA FUNDAMENTALS
1

2 THE PARTS OF A JAVA PROGRAM
// Program by: L. Thompson // A simple Java program that displays the message // Have a good semester! on a line on the computer screen public class DisplayMessage { public static void main(String[ ] args) System.out.println("Have a good semester!"); System.exit(0); } 2

3 THE PARTS OF A JAVA PROGRAM Comments
// Program by: L. Thompson // A simple Java program that displays the message // Have a good semester! on a line on the computer screen public class DisplayMessage { public static void main(String[ ] args) System.out.println("Have a good semester!"); System.exit(0); } These are comments. They are ignored by the compiler. 3

4 THE PARTS OF A JAVA PROGRAM Comments
Comments are notes of explanation used to document programs, sections of programs, or program statements for the humans who must read programs. Every program should start with a comment that supplies the name of the author of the program and a description of the program. DisplayMessage.java begins with the following comments: // Program by: L. Thompson // A simple Java program that displays the message // Have a good semester! on a line on the computer screen 4

5 THE PARTS OF A JAVA PROGRAM Comments
The compiler, the program that translates Java programs to byte code, ignores everything from the two forward slashes to the end of the line. Comments do not end with a semicolon. 5

6 THE PARTS OF A JAVA PROGRAM
// Program by: L. Thompson // A simple Java program that displays the message // Have a good semester! on a line on the computer screen public class DisplayMessage { public static void main(String[ ] args) System.out.println("Have a good semester!"); System.exit(0); } The compiler ignores blank lines. It is a good idea to use blank lines to make your program easier to read. 6

7 THE PARTS OF A JAVA PROGRAM Class Definition
// Program by: L. Thompson // A simple Java program that displays the message // Have a good semester! on a line on the computer screen public class DisplayMessage { public static void main(String[ ] args) System.out.println("Have a good semester!"); System.exit(0); } This is the class header for the class named DisplayMessage. This is a class definition. 7

8 THE PARTS OF A JAVA PROGRAM Class Definition
Every Java program must have at least one class definition. We will use a class as a container for the statements that comprise our program. Notice that the class named DisplayMessage contains the statements that make up our program. public class DisplayMessage { public static void main(String[ ] args) System.out.println("Have a good semester!"); System.exit(0); } 8

9 THE PARTS OF A JAVA PROGRAM Class Definition
public class DisplayMessage { public static void main(String[ ] args) System.out.println("Have a good semester!"); System.exit(0); } The word public is an access specifier that controls where the class can be accessed from. The key word public in the class header specifies that the class is unrestricted (i.e. the class can be accessed from any other class). 9

10 THE PARTS OF A JAVA PROGRAM Class Definition
No semicolon here! The class header is only the beginning of the class definition. public class DisplayMessage { public static void main(String[ ] args) System.out.println("Have a good semester!"); System.exit(0); } The key word class marks the beginning of the class. The class name, DisplayMessage, was chosen by the programmer. It is a user-defined identifier. The class header does not end with a semicolon. It is only the beginning of a class definition; it is not a complete statement. 10

11 THE PARTS OF A JAVA PROGRAM Class Definition
public class DisplayMessage { public static void main(String[ ] args) System.out.println("Have a good semester!"); System.exit(0); } A file can contain more that one class, but there cannot be more than one public class in a file. When a Java file contains a public class, the name of the public class and the filename (without the .java extension) must be the same. The source file containing the class shown above must be named DisplayMessage.java 11

12 THE PARTS OF A JAVA PROGRAM Class Definition
public class DisplayMessage { public static void main(String[ ] args) System.out.println("Have a good semester!"); System.exit(0); } The { is an opening brace. The } is the closing brace. All the statements in a class definition are enclosed in a set of braces {}. We say that the braces delimit the body of the class. It is considered good programming style to indent the statements inside a set of braces one level. 12

13 THE PARTS OF A JAVA PROGRAM Method Definition
// Program by: L. Thompson // A simple Java program that displays the message // Have a good semester! on a line on the computer screen public class DisplayMessage { public static void main(String[ ] args) System.out.println("Have a good semester!"); System.exit(0); } This is the method header for the method named main. This is a method definition. 13

14 THE PARTS OF A JAVA PROGRAM Method Definition
The method definition shown below is from DisplayMessage.java: public static void main(String[ ] args) { System.out.println("Have a good semester!"); System.exit(0); } A method is a named block of statements that perform a specific task when executed. Every Java application program must have a method called main. Java programs begin executing at the main method. 14

15 THE PARTS OF A JAVA PROGRAM Method Definition
The method definition shown below is from DisplayMessage.java: public static void main(String[ ] args) { System.out.println("Have a good semester!"); System.exit(0); } The method header is not a complete statement, so it does not end with a semicolon. The statements in a method must be enclosed in a set of braces {}. Again, indent the statements inside the braces. No semicolon here! The method header is only the beginning of the method definition. 15

16 THE PARTS OF A JAVA PROGRAM The println Method of the out Object of the System Class
// Program by: L. Thompson // A simple Java program that displays the message // Have a good semester! on a line on the computer screen public class DisplayMessage { public static void main(String[ ] args) System.out.println("Have a good semester!"); System.exit(0); } This statement tells the computer to display the message “Have a good semester!” on a line on the computer screen. 16

17 THE PARTS OF A JAVA PROGRAM The println Method of the out Object of the System Class
System.out.println("Have a good semester!"); A sequence of characters between the pair of quotation marks is called a string literal. The quotation marks delimit the string literal, they will not be displayed on the screen when the println method is executed. 17

18 Notice the semicolon here!
THE PARTS OF A JAVA PROGRAM The println Method of the out Object of the System Class System.out.println("Have a good semester!"); The println statement ends with a semicolon. It is a complete Java statement that instructs the computer to display a message on the computer screen. Notice the semicolon here! 18

19 THE PARTS OF A JAVA PROGRAM The exit Method of the System Class
// Program by: L. Thompson // A simple Java program that displays the message // Have a good semester! on a line on the computer screen public class DisplayMessage { public static void main(String[ ] args) System.out.println("Have a good semester!"); System.exit(0); } This statement tells the computer to end the execution of the process/program. 19

20 THE PARTS OF A JAVA PROGRAM The exit Method of the System Class
System.exit(0); The System classes exit method ends the execution of a process/program. The use of the exit method is optional here. When we display the output of our program in a dialog box, it will be necessary to use this method. 20

21 THE print AND println METHODS OF THE System
THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API Many of the programs we write will display console output. Console output is plain text. The standard output device is a console window. We will send strings of text to a console window using an object from the Java API. The Java API (Application Programmer Interface) is a standard library of prewritten classes available to Java programs for performing common operations. 21

22 THE print AND println METHODS OF THE System
THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API The program named DisplayMessage.java contains the statement: System.out.println("Have a good semester!"); This statement uses the System class from the Java API. The System class contains objects and methods that perform system level tasks. The out object is a member of the System class that contains the methods named print and println. These methods perform the task of writing strings of characters in a console window on the computer screen. 22

23 THE print AND println METHODS OF THE System
THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API Membership operators System.out.println("Have a good semester!"); The beginning of the line above is read System dot out dot print line. The dot is the membership operator in Java. It is used to specify that println is a member method of the out object which is a member of the System class. *** See Figure 2-3 from the text 23

24 THE print AND println METHODS OF THE System
THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API System.out.println("Have a good semester!"); We place the value that is to be displayed on the computer screen inside parentheses to pass it to the println method. A value passed to a method is called an argument. The argument passed to the println method in this example is the string literal "Have a good semester!". 24

25 THE print AND println METHODS OF THE System
THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API The println method displays a stream of characters and then advances the cursor to the beginning of the next line. The print method displays a stream of characters, but does not advance the cursor to the next line. 25

26 THE print AND println METHODS OF THE System
THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API ***See GettysburgA.java on webct 26

27 THE print AND println METHODS OF THE System
THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API ***See GettysburgB.java on webct 27

28 THE print AND println METHODS OF THE System
THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API Notice the difference in the output produced by GettysburgA.java and GettysburgB.java. The characters sent to the console are displayed in a continuous stream. A subsequent insertion begins where the previous insertion left off. Even if the output is broken into several print statements, one on each line, the output is displayed as one long continuous stream, unless we specify otherwise. Spaces we want included in the output must be included in the literals we send to the method. 28

29 THE print AND println METHODS OF THE System
THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API ***See GettysburgC.java on webct 29

30 THE print AND println METHODS OF THE System
THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API It is possible to mix the use of the print and println methods to get the desired output. 30

31 THE print AND println METHODS OF THE System
THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API ***See GettysburgD.java on webct 31

32 THE print AND println METHODS OF THE System
THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API Escape Sequences Another way to tell the computer to move the cursor to the beginning of a new line is to embed the newline escape sequence in a string literal at the point you want the new line to begin. The newline escape sequence is \n. In general, an escape sequence is a backslash, \, followed by one or more characters. Escape sequences are used to control the way output is displayed. 32

33 THE print AND println METHODS OF THE System
THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API Escape Sequences 33

34 THE print AND println METHODS OF THE System
THE print AND println METHODS OF THE System.out OBJECT AND THE JAVA API ***See GettysburgE.java on webct 34

35 VARIABLES A variable is a named location in memory that can store a data value that may be changed as the program executes. Part of the job of programming is determining what values need to be stored. The values stored in variables are often the inputs of the program and the results of calculations/processing. 35

36 VARIABLES In Java, variables must be declared before they are used. You declare a variable by writing a statement called a variable declaration. A variable declaration tells the compiler that storage is needed for a value of a particular data type. A variable declaration consists of the data type of the variable (the kind of data it can store) followed by the name of the variable. A variable declaration ends with a semicolon. 36

37 VARIABLES The statement below declares a variable named radius that can be used to store a double value. Variables of type double can store real numbers (numbers that may include a decimal point and a fractional portion). double radius; The declaration begins with the data type. The key word double is a data type for real numbers. 37

38 VARIABLES *** See the program AreaOf2Circles.java on webct 38

39 VARIABLES You may declare several variables of the same data type in a single statement by separating the variable names with commas. 39

40 VARIABLES The statement below declares four separate variables of data type int. Integer variables like these can store whole numbers. int payments, month, day, year; The statement above is equivalent to the group of statements below: int payments; int month; int day; int year; 40

41 VARIABLES In AreaOf2Circles.java we have the declarations:
double radius; double area; These statements could be replaced with the following statement: double radius, area; 41

42 VARIABLES Values may be assigned to variables using an assignment statement. An assignment copies the value of the expression on the right of the assignment operator into the location corresponding to the variable that is on the left of the assignment operator. 42

43 Note: The variable must be on the left side of the
VARIABLES Given the declaration: double radius; We can assign the value 3.1 to the variable named radius with the following statement: radius = 3.1; Note: The variable must be on the left side of the assignment operator = radius; is erroneous. 43

44 VARIABLES When a numeric variable is used in an arithmetic expression, the value stored in the memory location corresponding to the variable is used in the calculation. 44

45 VARIABLES The statement below is from AreaOf2Circles.java. When this statement is executed is multiplied by the value of radius and then the result of this calculation is multiplied by the value of radius and finally that result is stored in the area variable. area = * radius * radius; 45

46 VARIABLES When you send the name of a variable to the print or println method the contents of the variable are displayed. 46

47 VARIABLES The following statement from AreaOf2Circles.java displays the value in the area variable: System.out.println(area); 47

48 VARIABLES When using a variable, be sure not to enclose the name of the variable in quotation marks. Remember, a sequence of characters enclosed in quotation marks is a string literal, not the name of a variable. 48

49 VARIABLES The statement:
area = * radius * radius; From AreaOf2Circles.java, could not be written as: area = * "radius" * "radius"; // This is a syntax error The "radius" is a string literal. It is not legal to perform a mathematical operation on a string. 49

50 THE STRING CONCATENATION OPERATOR (+)
The + symbol represents two operations: The addition operation on numeric values The concatenation operation when at least one of its operands is a string The concatenation operator creates a new string containing the characters representing both of its operands. 50

51 THE STRING CONCATENATION OPERATOR (+)
In the statement below, the concatenation operator creates the new string "Hello World" which is then passed as the argument to the println method for display in a console window. System.out.println("Hello " + "World"); Note: A space character was placed inside the quotation marks after the word Hello. If this space was not there the string created by the concatenation operator would be “HelloWorld”. 51

52 THE STRING CONCATENATION OPERATOR (+)
If only one of the operands of the concatenation operator is a string, the other operand is converted to a string and then the two strings are concatenated. 52

53 THE STRING CONCATENATION OPERATOR (+)
For example, the following two statements are from the program AreaOf2Circles.java: System.out.print("The area of the first circle is "); System.out.println(area); These two statements could be replaced with the single statement: System.out.println("The area of the first circle is " + area); The resulting output is the same. 53

54 THE STRING CONCATENATION OPERATOR (+)
In this statement, the numeric value stored in the variable area is converted to a string and this string is concatenated to the string "The area of the first circle is ". If the value stored in area is , the new string created is "The area of the first circle is ". This string is the argument sent to the println method for display. System.out.println("The area of the first circle is " + area); 54

55 THE STRING CONCATENATION OPERATOR (+)
In Java, a string literal cannot extend over a line. The following statement will result in an error: System.out.println("Four score and seven years ago our fathers brought forth on this continent, a new nation,\nconceived in liberty, and dedicated to the proposition that all men are created equal."); It is an error to extend a string literal across a line. 55

56 THE STRING CONCATENATION OPERATOR (+)
We can use the concatenation operator to combine a number of smaller string literals into a single string. System.out.println("Four score and seven years ago our fathers brought forth " + "on this continent, a new nation,\n" + "conceived in liberty, and dedicated to the proposition that" + " all men are created equal."); Notice the style used when extending a statement over a line. Indent the second and subsequent lines. 56

57 LITERALS A literal is a constant value written in the code of a program. Literals are frequently used as: The values assigned to variables The operands in a arithmetic expressions Display values 57

58 LITERALS Highlighted below are some of the literals used in the program AreaOf2Circles.java: radius = 2; area = * radius * radius; System.out.print("The area of the first circle is "); 58

59 IDENTIFIERS An identifier is a programmer-defined name that corresponds to some element of a program. In this class the elements we will give names include: Variables and named constants The class containing our application program Methods 59

60 IDENTIFIERS Rules for Naming Identifiers
60

61 IDENTIFIERS Conventions for Naming Identifiers
It is good programming style to give your identifiers meaningful names. This means giving them names that give an indication of their usage. 61

62 IDENTIFIERS Conventions for Naming Variables
Write variable names using lowercase letters, capitalizing the first letter of the second and subsequent words that make up the variable name. This helps to visually break up the words since spaces cannot be used. Example: double accountBalance, interestRate; 62

63 IDENTIFIERS Conventions for Naming Classes
It is standard practice to begin a class name with an uppercase letter and capitalize the first letter of subsequent words that make up the class name. For example, notice the name of the class in the class header from the application program that calculates and displays the area of two circles: public class AreaOf2Circles This is the name of the class that is being defined. 63


Download ppt "Chapter 2 JAVA FUNDAMENTALS"

Similar presentations


Ads by Google