1 Training Lecture 1. 2 Primitive Data Declaration and Assignments Code State of Memory int...

Post on 17-Jan-2016

214 views 0 download

Tags:

Transcript of 1 Training Lecture 1. 2 Primitive Data Declaration and Assignments Code State of Memory int...

1

Training Lecture 1

2

Primitive Data Declaration and Assignments

Code State of Memory

int firstNumber, secondNumber;firstNumber = 234;secondNumber = 87;

AA

int firstNumber, secondNumber;int firstNumber, secondNumber;

BBfirstNumber = 234;firstNumber = 234;secondNumber = 87;secondNumber = 87;

int firstNumber, secondNumber;int firstNumber, secondNumber;firstNumber = 234;firstNumber = 234;secondNumber = 87;secondNumber = 87;

firstNumber

secondNumber

A. A. Variables are allocated in memory.

A. A. Variables are allocated in memory.

B. B. Values are assigned to variables.

B. B. Values are assigned to variables.

234

87

3

Assigning Numerical Data

Code State of Memory

int number;int number;number = 237;number = 237;number = 35;number = 35; number

A. A. The variable is allocated in memory.

A. A. The variable is allocated in memory.

B. B. The value 237237 is assigned to numbernumber.

B. B. The value 237237 is assigned to numbernumber.

237

int number;int number;

number = 237;number = 237;

number = 35;number = 35;

AAint number;int number;

BBnumber = 237;number = 237;

CCnumber = 35;number = 35;

C. C. The value 3535 overwrites the

previous value 237.237.

C. C. The value 3535 overwrites the

previous value 237.237.

35

Assigning Objects

Code State of Memory

Customer var;Customer var;var = new Customer( );var = new Customer( );var = new Customer( );var = new Customer( );

var

A. A. The variable is allocated in memory.

A. A. The variable is allocated in memory.Customer var;Customer var;

var = new Customer( );var = new Customer( );

var = new Customer( );var = new Customer( );

AA

Customer var;Customer var;BB

var = new Customer( );var = new Customer( );

CC

var = new Customer( );var = new Customer( );B. B. The reference to the

new object is assigned to varvar.

B. B. The reference to the new object is assigned to varvar.

CustomerCustomer

C. C. The reference to another object overwrites the reference in var.var.

C. C. The reference to another object overwrites the reference in var.var.

CustomerCustomer

Having Two References to a Single Object

Code State of Memory

Customer clemens, twain;Customer clemens, twain;clemens = new Customer( );clemens = new Customer( );twain = clemens;twain = clemens;

Customer clemens, twain,Customer clemens, twain,

clemens = new Customer( );clemens = new Customer( );

twain = clemens;twain = clemens;

AA

Customer clemens, twain;Customer clemens, twain; BB

clemens = new Customer( );clemens = new Customer( );

CC

twain = clemens;twain = clemens;

A. A. Variables are allocated in memory.

A. A. Variables are allocated in memory.

clemens

twain

B. B. The reference to the new object is assigned to clemensclemens.

B. B. The reference to the new object is assigned to clemensclemens.

CustomerCustomer

C. C. The reference in clemensclemens is assigned to

customer.customer.

C. C. The reference in clemensclemens is assigned to

customer.customer.

Object Creation

myWindow = new JFrame ( ) ;

MoreExamples

Customer Customer customer;customer = new Customer( );jon = new Student(“John Java”);car1 = new Vehicle( );

Object NameName of the object we are creating here.

Object NameName of the object we are creating here.

Class NameAn instance of this class is created.

Class NameAn instance of this class is created.

ArgumentNo arguments are used here.

ArgumentNo arguments are used here.

Declaration vs. Creation

Customer customer;

customer = new Customer( );

Customer customer;

customer = new Customer( );

1. The identifier customer is declared and space is allocated in memory.

1. The identifier customer is declared and space is allocated in memory.

2. A Customer object is created and the identifier customer is set to refer to it.

2. A Customer object is created and the identifier customer is set to refer to it.

1

2

customer

1

: Customer2

State-of-Memory vs. Program

customer

: Customer

State-of-MemoryNotation

customer : Customer

Program DiagramNotation

Class NameClass NameObject NameObject Name

Name vs. Objects

Customer customer;

customer = new Customer( );

customer = new Customer( );

Customer customer;

customer

customer = new Customer( );

customer = new Customer( );

: Customer : CustomerCreated with the first new.

Created with the first new.

Created with the second new. Reference to the first Customer object is lost.

Created with the second new. Reference to the first Customer object is lost.

JFrame myWindow;

myWindow = new JFrame( );

myWindow.setSize(300, 200);

myWindow.setTitle (“My First Java Program”);

myWindow.setVisible(true);

Execution Flow

myWindow.setSize(300, 200);

Jframe myWindow;

myWindow

myWindow.setVisible(true);

State-of-Memory Diagram

: JFrame

width

height

title

visible

200

My First Java …

300

true

myWindow = new JFrame( );

myWindow.setTitle (“My First Java Program”);

The diagram shows only four of the many data members of a JFrame object.

The diagram shows only four of the many data members of a JFrame object.

Program Code

4th Ed Chapter 5 - 18

Using == With Objects (Sample 1)String str1 = new String("Java");String str2 = new String("Java");

if (str1 == str2) {System.out.println("They are equal");

} else {System.out.println("They are not equal");

}

They are not equal

Not equal because str1 and str2 point to different String objects.

4th Ed Chapter 5 - 19

Using == With Objects (Sample 2)String str1 = new String("Java");String str2 = str1;

if (str1 == str2) {System.out.println("They are equal");

} else {System.out.println("They are not equal");

}

They are equal It's equal here because str1 and str2 point to the same object.

4th Ed Chapter 5 - 20

Using equals with String

String str1 = new String("Java");String str2 = new String("Java");

if (str1.equals(str2)) {System.out.println("They are equal");

} else {System.out.println("They are not equal");

}

They are equal It's equal here because str1 and str2 have the same sequence of characters.

4th Ed Chapter 5 - 21

The Semantics of ==

4th Ed Chapter 5 - 22

In Creating String Objects

Example 1

24

25

26

Example 2

enter the number of Sayarat that you want to process 3enter the owner then price then model you want to processali87000teriosali 's Car ,price : 87000.0model : terios

The tax will be 1000 SR enter the owner then price then model you want to process mohamed90000camrymohamed 's Car ,price : 90000.0 model : camry

The tax will be 1000 SR enter the owner then price then model you want to process

31

END of

Training Lecture 1