1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple...

63
1 Chapter 2 JAVA FUNDAMENTALS

Transcript of 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple...

Page 1: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

1

Chapter 2JAVA FUNDAMENTALS

Page 2: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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

}

}

Page 3: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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.

Page 4: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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

Page 5: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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.

Page 6: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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.

Page 7: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

7

THE PARTS OF A JAVA PROGRAMClass 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.

Page 8: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

8

THE PARTS OF A JAVA PROGRAMClass 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);

}

}

Page 9: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

9

THE PARTS OF A JAVA PROGRAMClass 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).

Page 10: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

10

THE PARTS OF A JAVA PROGRAMClass 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.

No semicolon here! The class header is only the beginning of the class

definition.

Page 11: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

11

THE PARTS OF A JAVA PROGRAMClass 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

Page 12: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

12

THE PARTS OF A JAVA PROGRAMClass 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.

Page 13: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

13

THE PARTS OF A JAVA PROGRAMMethod 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.

Page 14: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

14

THE PARTS OF A JAVA PROGRAMMethod 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.

Page 15: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

15

THE PARTS OF A JAVA PROGRAMMethod 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.

Page 16: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

16

THE PARTS OF A JAVA PROGRAMThe 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.

Page 17: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

17

THE PARTS OF A JAVA PROGRAMThe 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.

Page 18: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

18

THE PARTS OF A JAVA PROGRAMThe 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!

Page 19: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

19

THE PARTS OF A JAVA PROGRAMThe 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.

Page 20: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

20

THE PARTS OF A JAVA PROGRAMThe 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.

Page 21: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

21

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.

Page 22: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

22

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.

Page 23: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

23

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

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

Membership operators

Page 24: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

24

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!".

Page 25: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

25

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.

Page 26: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

26

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

***See GettysburgA.java on webct

Page 27: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

27

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

***See GettysburgB.java on webct

Page 28: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

28

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.

Page 29: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

29

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

***See GettysburgC.java on webct

Page 30: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

30

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.

Page 31: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

31

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

***See GettysburgD.java on webct

Page 32: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

32

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.

Page 33: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

33

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

Causes a double quotation mark to be printeddouble quote\”

Causes a single quotation mark to be printedsingle quote\’

Causes a backslash to be printedbackslash\\

Causes the cursor to go to the beginning of the current line, notthe next line

carriage return\r

Causes the cursor to back up, or move left, one positionbackspace\b

Causes the cursor to skip over to the next tab stoptab\t

Advances the cursor to the next line for subsequent printingnewline\n

Page 34: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

34

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

***See GettysburgE.java on webct

Page 35: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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.

Page 36: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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.

Page 37: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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.

Page 38: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

38

VARIABLES

*** See the program AreaOf2Circles.java on webct

Page 39: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

39

VARIABLES

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

Page 40: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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;

Page 41: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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;

Page 42: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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.

Page 43: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

43

VARIABLES

Given the declaration:

double radius;

We can assign the value 3.1 to the variable named radius withthe following statement:

radius = 3.1;

Note: The variable must be on the left side of theassignment operator. 3.1 = radius; is erroneous.

Page 44: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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.

Page 45: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

45

VARIABLES

The statement below is from AreaOf2Circles.java. When this statement is executed 3.14159 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 = 3.14159 * radius * radius;

Page 46: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

46

VARIABLES

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

Page 47: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

47

VARIABLES

The following statement from AreaOf2Circles.java displays the value in the area variable:

System.out.println(area);

Page 48: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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.

Page 49: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

49

VARIABLES

The statement:

area = 3.14159 * radius * radius;

From AreaOf2Circles.java, could not be written as:

area = 3.14159 * "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.

Page 50: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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.

Page 51: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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

Page 52: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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.

Page 53: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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.

Page 54: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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 12.56636, the new string created is "The area of the first circle is 12.56636". This string is the argument sent to the println method for display.

System.out.println("The area of the first circle is " + area);

Page 55: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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.

Page 56: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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.

Page 57: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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

Page 58: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

58

LITERALS

Highlighted below are some of the literals used in the program AreaOf2Circles.java:

radius = 2;

area = 3.14159 * radius * radius;

System.out.print("The area of the first circle is ");

Page 59: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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

Page 60: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

60

IDENTIFIERS Rules for Naming Identifiers

There are some rules that must be followed when creating thenames of variables and other identifiers.

1. Do not use any of the Java key words as identifiers.2. The first character of an identifier must be an a-z, A-Z, an

underscore character (_), or the dollar sign ($).3. After the first character you can use letters a-z or A-Z, digits

0-9, underscores, or dollar signs.4. Spaces are not allowed in identifiers.

Page 61: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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.

Page 62: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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;

Page 63: 1 Chapter 2 JAVA FUNDAMENTALS. 2 THE PARTS OF A JAVA PROGRAM // Program by: L. Thompson // A simple Java program that displays the message // Have a good.

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.