2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

39
1 2005 Pearson Education, Inc. All rights rese 1 Introduction

Transcript of 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

Page 1: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

1

2005 Pearson Education, Inc. All rights reserved.

11

Introduction

Page 2: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

2

2005 Pearson Education, Inc. All rights reserved.

History of Java

• Java– Originally for intelligent consumer-electronic devices

– Then used for creating Web pages with dynamic content

– Now also used to:• Develop large-scale enterprise applications

• Enhance WWW server functionality

• Provide applications for consumer devices (cell phones, etc.)

Page 3: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

3

2005 Pearson Education, Inc. All rights reserved.

Java Class Libraries

• Classes– Include methods that perform tasks

• Return information after task completion

– Used to build Java programs

• Java provides class libraries– Known as Java APIs (Application Programming

Interfaces)

Page 4: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

4

2005 Pearson Education, Inc. All rights reserved.

When programming in Java, you will typically use the following building blocks: Classes and methods from class libraries, classes and methods you create yourself and classes and methods that others create and make available to you.

Page 5: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

5

2005 Pearson Education, Inc. All rights reserved.

Typical Java Development Environment

• Java programs normally undergo five phases– Edit

• Programmer writes program (and stores program on disk)

– Compile• Compiler creates bytecodes from program

– Load• Class loader stores bytecodes in memory

– Execute• JVM translates bytecodes into machine language

Page 6: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

6

2005 Pearson Education, Inc. All rights reserved.

OBJECTIVES

In this chapter you will learn:

• To write simple Java applications.

• To use input and output statements.

• Java’s primitive types.

• Basic memory concepts.

• To use arithmetic operators.

• The precedence of arithmetic operators.

• To write decision-making statements.

• To use relational and equality operators.

Page 7: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

7

2005 Pearson Education, Inc. All rights reserved.

Outline

•Welcome1.java

1 // Fig. 2.1: Welcome1.java

2 // Text-printing program.

3

4 public class Welcome1

5 {

6 // main method begins execution of Java application

7 public static void main( String args[] )

8 {

9 System.out.println( "Welcome to Java Programming!" );

10

11 } // end method main

12

13 } // end clazss Welcome1

Welcome to Java Programming!

Page 8: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

8

2005 Pearson Education, Inc. All rights reserved.

First Program in Java: Printing a Line of Text (Cont.)

– Comments start with: //• Comments ignored during program execution

• Document and describe code

• Provides code readability

– Traditional comments: /* ... *//* This is a traditional comment. It can be split over many lines */

– Another line of comments

1 // Fig. 2.1: Welcome1.java

2 // Text-printing program.

Page 9: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

9

2005 Pearson Education, Inc. All rights reserved.

First Program in Java: Printing a Line of Text (Cont.)

– Begins class declaration for class Welcome1• Every Java program has at least one user-defined class

• Keyword: words reserved for use by Java

– class keyword followed by class name

• Naming classes: capitalize every word

– SampleClassName

4 public class Welcome1

Page 10: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

10

2005 Pearson Education, Inc. All rights reserved.

First Program in Java: Printing a Line of Text (Cont.)

– Java identifier• Series of characters consisting of letters, digits,

underscores ( _ ) and dollar signs ( $ )

• Does not begin with a digit, has no spaces

• Examples: Welcome1, $value, _value, button7

– 7button is invalid

• Java is case sensitive (capitalization matters)

– a1 and A1 are different

4 public class Welcome1

Page 11: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

11

2005 Pearson Education, Inc. All rights reserved.

First Program in Java: Printing a Line of Text (Cont.)

– Saving files• File name must be class name with .java extension

• Welcome1.java

– Left brace {• Begins body of every class

• Right brace ends declarations (line 13)

4 public class Welcome1

5 {

Page 12: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

12

2005 Pearson Education, Inc. All rights reserved.

Common Programming Error

• It is an error not to end a file name with the .java extension for a file containing a class declaration. If that extension is missing, the Java compiler will not be able to compile the class declaration.

Page 13: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

13

2005 Pearson Education, Inc. All rights reserved.

First Program in Java: Printing a Line of Text (Cont.)

– Part of every Java application• Applications begin executing at main

– Parentheses indicate main is a method (Ch. 3 and 6)– Java applications contain one or more methods

• Exactly one method must be called main

– Methods can perform tasks and return information• void means main returns no information

– Left brace begins body of method declaration• Ended by right brace } (line 11)

7 public static void main( String args[] )

8 {

Page 14: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

14

2005 Pearson Education, Inc. All rights reserved.

First Program in Java: Printing a Line of Text (Cont.)

– Instructs computer to perform an action• Prints string of characters

– String - series characters inside double quotes• White-spaces in strings are not ignored by compiler

– System.out• Standard output object• Print to command window (i.e., MS-DOS prompt)

– Method System.out.println • Displays line of text

– This line known as a statement• Statements must end with semicolon ;

9 System.out.println( "Welcome to Java Programming!" );

Page 15: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

15

2005 Pearson Education, Inc. All rights reserved.

Common Programming Error

• Omitting the semicolon at the end of a statement is a syntax error.

Page 16: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

16

2005 Pearson Education, Inc. All rights reserved.

First Program in Java: Printing a Line of Text (Cont.)

– Ends method declaration

– Ends class declaration

11 } // end method main

13 } // end class Welcome1

Page 17: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

17

2005 Pearson Education, Inc. All rights reserved.

Modifying Our First Java Program (Cont.)

• Modifying programs

-Displays “Welcome to ” with cursor remaining on printed line

-Displays “Java Programming! ” on same line with cursor on next line

System.out.print( "Welcome to " ); System.out.println( "Java Programming!" );

Page 18: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

18

2005 Pearson Education, Inc. All rights reserved.

Some common escape sequences.

Escape sequence

Description

\n Newline. Position the screen cursor at the beginning of the next line.

\t Horizontal tab. Move the screen cursor to the next tab stop. \\ Backslash. Used to print a backslash character. \" Double quote. Used to print a double-quote character. For example,

System.out.println( "\"in quotes\"" );

displays

"in quotes"

Page 19: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

19

2005 Pearson Education, Inc. All rights reserved.

Outline 1 // Fig. 2.4: Welcome3.java

2 // Printing multiple lines of text with a single statement.

3

4 public class Welcome3

5 {

6 // main method begins execution of Java application

7 public static void main( String args[] )

8 {

9 System.out.println( "Welcome\nto\nJava\nProgramming!" );

10

11 } // end method main

12

13 } // end class Welcome3

Welcome to Java Programming!

Page 20: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

20

2005 Pearson Education, Inc. All rights reserved.

Displaying Text with printf

•System.out.printf– Displays formatted data– Format specifier %s – placeholder for a string

Welcome to

Java Programming!

System.out.printf("%s\n%s\n","Welcome to","Java Programming!" );

Page 21: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

21

2005 Pearson Education, Inc. All rights reserved.

Another Java Application: Adding Integers

• Upcoming program

– Use Scanner to read two integers from user

– Use printf to display sum of the two values

– Use packages

Page 22: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

22

2005 Pearson Education, Inc. All rights reserved.

Outline 1 // Fig. 2.7: Addition.java

2 // Addition program that displays the sum of two numbers.

3 import java.util.Scanner; // program uses class Scanner

4

5 public class Addition

6 {

7 // main method begins execution of Java application

8 public static void main( String args[] )

9 {

10 // create Scanner to obtain input from command window

11 Scanner input = new Scanner( System.in );

12

13 int number1; // first number to add

14 int number2; // second number to add

15 int sum; // sum of number1 and number2

16

17 System.out.print( "Enter first integer: " ); // prompt

18 number1 = input.nextInt(); // read first number from user

19

Page 23: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

23

2005 Pearson Education, Inc. All rights reserved.

Outline20 System.out.print( "Enter second integer: " ); // prompt

21 number2 = input.nextInt(); // read second number from user

22

23 sum = number1 + number2; // add numbers

24

25 System.out.printf( "Sum is %d\n", sum ); // display sum

26

27 } // end method main

28

29 } // end class Addition

Enter first integer: 45 Enter second integer: 72 Sum is 117

Page 24: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

24

2005 Pearson Education, Inc. All rights reserved.

Another Java Application: Adding Integers (Cont.)

– import declarations • Used by compiler to identify and locate classes used in Java

programs

• Tells compiler to load class Scanner from java.util package

– Begins public class Addition

3 import java.util.Scanner; // program uses class Scanner

5 public class Addition 6 {

Page 25: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

25

2005 Pearson Education, Inc. All rights reserved.

Common Programming Error

• All import declarations must appear before the first class declaration in the file. Placing an import declaration inside a class declaration’s body or after a class declaration is a syntax error.

Page 26: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

26

2005 Pearson Education, Inc. All rights reserved.

Error-Prevention Tip

• Forgetting to include an import declaration for a class used in your program typically results in a compilation error containing a message such as “cannot resolve symbol.”

Page 27: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

27

2005 Pearson Education, Inc. All rights reserved.

Another Java Application: Adding Integers (Cont.)

– Variable Declaration Statement

•Input is of type Scanner – Enables a program to read data for use

•System.in Standard input object• Declarations end with semicolons ;

10 //create Scanner to obtain input from command window11 Scanner input = new Scanner( System.in );

Page 28: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

28

2005 Pearson Education, Inc. All rights reserved.

Another Java Application: Adding Integers (Cont.)

– Declare variable number1, number2 and sum of type int• int holds integer values (whole numbers): i.e., 0, -4, 97• Types float and double can hold decimal numbers

• Type char can hold a single character: i.e., x, $, \n, 7

– Can declare multiple variables of the same type in one declaration

13 int number1; // first number to add14 int number2; // second number to add15 int sum; // second number to add

int number1,number2,sum;

Page 29: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

29

2005 Pearson Education, Inc. All rights reserved.

Another Java Application: Adding Integers (Cont.)

– Message called a prompt - directs user to perform an action– Result of call to nextInt given to number1 using

assignment operator =

• Assignment statement– = binary operator - takes two operands

• Expression on right evaluated and assigned to variable on left• Read as: number1 gets the value of input.nextInt()

17 System.out.print( "Enter first integer:" );

18 number1 = input.nextInt(); // read first number from user

Page 30: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

30

2005 Pearson Education, Inc. All rights reserved.

Software Engineering Observation

• By default, package java.lang is imported in every Java program; thus, java.lang is the only package in the Java API that does not require an import declaration.

Page 31: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

31

2005 Pearson Education, Inc. All rights reserved.

Another Java Application: Adding Integers (Cont.)

• Prompts the user to input the second integer

• Assign variable number2 to second integer input

-Assignment statement• Calculates sum of number1 and number2 (right hand side)• Uses assignment operator = to assign result to variable sum• Read as: sum gets the value of number1 + number2• number1 and number2 are operands

20 System.out.print( "Enter second integer: " );

21 number2 = input.nextInt();

23 sum = number1 + number2;

Page 32: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

32

2005 Pearson Education, Inc. All rights reserved.

Another Java Application: Adding Integers (Cont.)

– Use System.out.printf to display results

– Format specifier %d• Placeholder for an int value

25 System.out.printf( "Sum is %d\n: " , sum );

System.out.printf( "Sum is %d\n: " ,( number1 + number2 ) );

Page 33: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

33

2005 Pearson Education, Inc. All rights reserved.

Memory Concepts

• Variables – Every variable has a name, a type, a size and a value

• Name corresponds to location in memory

– When new value is placed into a variable, replaces (and destroys) previous value

– Reading variables from memory does not change them

Page 34: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

34

2005 Pearson Education, Inc. All rights reserved.

Memory location showing the name and value of variable number1.

Page 35: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

35

2005 Pearson Education, Inc. All rights reserved.

Memory locations after storing values for number1 and number2.

Page 36: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

36

2005 Pearson Education, Inc. All rights reserved.

Memory locations after calculating and storing the sum of number1 and number2.

Page 37: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

37

2005 Pearson Education, Inc. All rights reserved.

Arithmetic operators.

Java operation

Arithmetic operator

Algebraic expression

Java expression

Addition + f + 7 f + 7

Subtraction – p – c p - c

Multiplication * Bm b * m

Division / x / y or or x ÷ y

x / y

Page 38: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

38

2005 Pearson Education, Inc. All rights reserved.

Equality and relational operators.

Standard algebraic equality or relational operator

Java equality or relational operator

Sample Java condition

Meaning of Java condition

Equality operators == x == y x is equal to y != x != y x is not equal to y Relational operators > x > y x is greater than y < x < y x is less than y >= x >= y x is greater than or equal to y ≤ <= x <= y x is less than or equal to y

Page 39: 2005 Pearson Education, Inc. All rights reserved. 1 1 1 Introduction.

39

2005 Pearson Education, Inc. All rights reserved.

Precedence and associatively of operations discussed.

Operators Associativity Type

* / % left to right multiplicative

+ - left to right additive

< <= > >= left to right relational

== != left to right equality

= right to left assignment