59
Using Objects

Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

Embed Size (px)

Citation preview

Page 1: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

Using Objects

Page 2: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 2

Java and Objects

In Java, everything is an object Programs are objects Objects (including programs and

objects within programs)CREATE and USE other objects

Page 3: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 3

How do you USE objects?public class BankDemo1{ public static void main( String args[] ) { BankAccount bobAcct; bobAcct = new BankAccount(); int balance1 = bobAcct.getBalance(); System.out.println( "Bob's balance is now: " + balance1 ); System.out.println( "Depositing 50 to Bob's account ..." );

bobAcct.deposit( 50 ); System.out.println( "Bob's balance is now: " + bobAcct.getBalance() ); }}

DECLARE a variable that can “point” to a BankAccount

CREATE a new BankAccount and make account point to it

CALL getBalance() method of account. Returns an int

Call deposit method with amount as argument.

CALL getBalance() method again to get the balance again

Page 4: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 4

Declaration vs. Creation

Why do you still need to create the object after declaring it?

Because object-type variables are reference variables (aka pointers)

They don’t store the object itself, they just point to it

Page 5: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 5

Declaration vs. Creation

BankAccount bobAcct;

bobAcct = new BankAccount( );Declaration allocatesa reference(aka “pointer”)

But it does NOT create an object.

Reference is “null”,i.e., points to nothing

bobAcct

Page 6: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 6

Declaration vs. Creation

bobAcct

Creating an object …• “new” creates an instance• “=” makes variable point to it

BankAccount bobAcct;

bobAcct = new BankAccount( );

BankAccount

int balance

0

Page 7: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 7

Reference Variables Why use references? Why not just store the object in the variable

itself (like with primitive types such as ints, doubles, etc.)?

Answer: objects often need to be “passed around” and

used by other objects two objects can need to use the same object at

the same time e.g., the client and the bank teller both need to

have access to the same BankAccount instance

Page 8: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 8

Example: Two Accounts

bobAcct

BankAccount bobAcct;

bobAcct = new BankAccount( );

BankAccount aliceAcct;

aliceAcct = new

BankAccount();

bobAcct.deposit( 50 );

aliceAcct.deposit( 60 );

BankAccount

int balance

0

aliceAcctBankAccount

int balance

050 60

Page 9: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 9

Example: “Joint Account”

bobAcct

BankAccount bobAcct;

bobAcct = new BankAccount( );

BankAccount aliceAcct;

aliceAcct = bobAcct;

bobAcct.deposit( 50 );

aliceAcct.deposit( 60 );

BankAccount

int balance

0

aliceAcct

50110

Page 10: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 10

Something to think about Supposeint a;a = 150000;int b = a;a = a + 300000;int c = b;…BankAccount x = new BankAccount();x.deposit( 150000 );BankAccount y = x;x.deposit( 300000 );long z = y.getBalance();

What is the final value of c? z? Why? Write a program that tests the code

above.

Page 11: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 11

public class BankDemo1{ public static void main( String args[] ) { BankAccount bobAcct; int balance1 = bobAcct.getBalance(); System.out.println( "Bob's balance is now: " + balance1 ); System.out.println( "Depositing 50 to Bob's account ..." );

bobAcct.deposit( 50 ); System.out.println( "Bob's balance is now: " + bobAcct.getBalance() ); }}

A Common Error

Forgot to create a new BankAccount and assign it to account

What will go wrong?

Page 12: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 12

public class BankDemo1{ public static void main( String args[] ) { BankAccount bobAcct; int balance1 = bobAcct.getBalance(); System.out.println( "Bob's balance is now: " + balance1 ); System.out.println( "Depositing 50 to Bob's account ..." );

bobAcct.deposit( 50 ); System.out.println( "Bob's balance is now: " + bobAcct.getBalance() ); }}

A Common Error

You will get aNullPointerException!

Forgot to create a new BankAccount and assign it to account

Page 13: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 13

Calling a Method on a Null Pointer

NullPointerException: when you try to call a method or access a field on an object variable that is not pointing to anything

Remember that you need to create a new object before using it!

account

nullnull

deposit( 250 )

account

deposit( 250 )

BankAccount

??

Page 14: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 14

The Balloon Analogy

Think of Objects as balloons! references are like strings (aka “handles”) Idea from “Beginning Java Objects” by Jacquie

Barker (Wrox)

x y z(null) (null) (null)

BankAccount x, y, z;

x = new BankAccount();

y = x;

z = new BankAccount();

y = z;

x = z;

Page 15: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 15

The Balloon Analogy

x y z(null) (null)

BankAccount x, y, z;

x = new BankAccount();

y = x;

z = new BankAccount();

y = z;

x = z;

Think of Objects as balloons! references are like strings (aka “handles”) Idea from “Beginning Java Objects” by Jacquie

Barker (Wrox)

Page 16: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 16

The Balloon Analogy

x y z(null)

BankAccount x, y, z;

x = new BankAccount();

y = x;

z = new BankAccount();

y = z;

x = z;

Think of Objects as balloons! references are like strings (aka “handles”) Idea from “Beginning Java Objects” by Jacquie

Barker (Wrox)

Page 17: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 17

The Balloon Analogy

x y z

BankAccount x, y, z;

x = new BankAccount();

y = x;

z = new BankAccount();

y = z;

x = z;

Think of Objects as balloons! references are like strings (aka “handles”) Idea from “Beginning Java Objects” by Jacquie

Barker (Wrox)

Page 18: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 18

The Balloon Analogy

BankAccount x, y, z;

x = new BankAccount();

y = x;

z = new BankAccount();

y = z;

x = z;

x y z

Think of Objects as balloons! references are like strings (aka “handles”) Idea from “Beginning Java Objects” by Jacquie

Barker (Wrox)

Page 19: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 19

The Balloon Analogy

BankAccount x, y, z;

x = new BankAccount();

y = x;

z = new BankAccount();

y = z;

x = z;

x y z

Think of Objects as balloons! references are like strings (aka “handles”) Idea from “Beginning Java Objects” by Jacquie

Barker (Wrox)

Page 20: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 20

Think of Objects as balloons! references are like strings (aka “handles”) Idea from “Beginning Java Objects” by Jacquie

Barker (Wrox)

The Balloon Analogy

BankAccount x, y, z;

x = new BankAccount();

y = x;

z = new BankAccount();

y = z;

x = z;

x y z

If there is no reference pointing to an object anymore, it gets “garbage collected”

Page 21: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 21

Points to Remember Remember: Object variables

are references (aka pointers) Point to “null” by default Need to call new to create

a new object Different variables can point to same

object at the same time If no pointers point to an object, then it

gets collected by the “garbage collector”

You can’t access it anyway if you don’t have a pointer to it

This allows Java to automatically save on memory

Page 22: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

More on using Methods

Page 23: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 23

How Methods WorkBankProgram.java

State of Memory

…int aliceBalance = aliceAccount.getBalance(); …

public int getBalance(){ return this.balance;}

BankAccount.java

aliceAccount

getBalance()

BankAccount

aliceBalance

100int balance

1) Find object pointed to by aliceAccount

2) Find code for class of that object

3) Find code for getBalance() 4) Run code5) Return value6) Use returned value

When the line above is run …

100

100

Page 24: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 24

Three Kinds of Variables Field (aka Attribute or Instance Variable)

Variables declared inside a class’ code, but outside any methods Part of object’s “permanent” state Use for state that is retained between method calls can be accessed as “this.fieldname” or simply fieldname

Local Variable Variables declared inside a method definition Only exists while we’re inside the method Use as a “scratchpad” (temporary storage) during a computation

Parameter Variables declared in the parentheses of a method definition Holds a copy of the value or reference

passed as an argument to the method call* Is also a local variable – i.e., only exists inside the method

* Note: the argument is the variable or value given in the method call, while the parameter is the variable in the method definition that copies that value)

Page 25: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 25

BankAccount Example

BankAccount

int balance

BankAccount() (constructor)

int getBalance()void deposit( int amount )

public class BankAccount{ private int balance;

public BankAccount() { this.balance = 0; }

public int getBalance() { return this.balance; }

public void deposit( int amount ) { this.balance = this.balance + amount; } …}

BankAccount.java

acct1: BankAccount

0balance

Suppose acct1 already exists,and another object calls …

acct1.deposit( 100 );

What happens?

A variable named amount is created with value 100 and passed as the parameter to deposit. (It also becomes a local variable within deposit.)

Read the value of this.balance, add to value of amount, and store the value into this.balance again

100

amount0 100

100100

Page 26: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 26

Using the Debugger in BlueJ

In BlueJ, set a breakpoint in your main class (the one with main())

Then, try using “Step Into” BlueJ allows you to watch the local

variables and the fields as they are created/disappear, and as they change values

Page 27: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 27

Scoping Rules

Variable is visible within the block (curly brace pairs) in which it is declared field is visible within the whole class, local variable is visible within the method, “sub-local” variables declared within blocks

inside a method are only visible within their block

You can’t declare a variable of the same name twice in the same block

But, you can declare a variable of the same name in different blocks

This creates two totally independent variables

Page 28: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 28

Variables of the Same Name

Non-Overlapping Blocks: no conflict since variables do not exist at the same time

parameters or local variables in different methods e.g., int amount parameter in deposit and withdraw

Overlapping Blocks: innermost variable is visible, outer variable(s) are “hidden”

e.g., a field int x vs. a local variable int x inside the method, the name x refers to the local variable but remember that the field x still exists, and has a

different value This is why the parameters of constructors should have

different names from the fields they initialize, unless you’re using “this”

Page 29: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 29

State of Memory

private void myMethod( int x, float y ){ x = 25; y = 35.4f;}

AA

A. A. Local variables do not exist before the method execution

A. A. Local variables do not exist before the method execution

At before myMethodAA

Codex = 10;y = 20;myMethod( x, y );

xx 1010

y 1020

Assume x

and y are

fields

Assume this is a

convenience method in the same

class

Variables of the Same Name

Page 30: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 30

State of Memory

B. B. Note that the parameters are totally independent storage spaces.

B. B. Note that the parameters are totally independent storage spaces.

Values are copied at BB

Codex = 10;y = 20;myMethod( x, y ); BB

xx 1010

y 1020

x 1010

y 1020.0f

private void myMethod( int x, float y ){ x = 25; y = 35.4f;}

Variables of the Same Name

Page 31: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 31

CC

State of Memory

C. C. The values of parameters are

changed.

C. C. The values of parameters are

changed.

After is executedCC

Codex = 10;y = 20;myMethod( x, y );

xx 1010

y 1020

x 1025

y 1035.4f

private void myMethod( int x, float y ){ x = 25; y = 35.4f;}

Variables of the Same Name

Page 32: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 32

Code

DD

State of Memory

D. D. Parameters are erased. Arguments remain unchanged.

D. D. Parameters are erased. Arguments remain unchanged.

At after myMethodDD

x = 10;y = 20;myMethod( x, y );

xx 1010

y 1020

private void myMethod( int x, float y ){ x = 25; y = 35.4f;}

Variables of the Same Name

Page 33: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 33

Constructors A constructor is a special kind of method that is

called with the new command Used for initializing an object to a valid state Name of constructor must be same as name of class No return type (!)

if you accidentally put a return type, compiler will not complain. It will define an ordinary method which happens to have the same name as the class

Default “no-args” constructor If no constructor is defined, the Java compiler will include a

default constructor with no arguments and no body If a constructor is defined, then the compiler will not

create a “no-args” constructor

Page 34: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 34

Defining Constructors A constructor will have the following form:

public <class name> ( <parameters> ){

<statements>}

public BankAccount ( int initialBalance )

{

this.balance = initialBalance;

}

StatementsStatements

ModifierModifier Class NameClass Name ParameterParameter

Note:No

Return Type!

Note:No

Return Type!

Page 35: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 35

What’s Wrong?

public class StudentRecord

{

private String name;

public StudentRecord( String name )

{

}

… methods (not shown) …

}

Some people think that you can set a field by simply giving the parameter same name as the field. THIS DOES NOT WORK.

The parameter and the field are two different and independent variables, even if they have the same name.

Page 36: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 36

The Correct Way

public class StudentRecord

{

private String name;

public StudentRecord( String initialName )

{

name = initialName;

}

… methods (not shown) …

}

Give the parameter a different name in order to be clear.

Don’t forget to set the field through an assignment statement.

Page 37: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 37

Another Way

public class StudentRecord

{

private String name;

public StudentRecord( String name )

{

this.name = name;

}

… methods (not shown) …

}

Give the parameter the same name as the field. (Convenient!)

Use “this” to distinguish between “name” the field and “name” the parameter(But be careful! What happens if you forget to say this?)

Page 38: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 38

Another Error to be careful of

public class StudentRecord

{

private String name;

public StudentRecord( String Name )

{

this.name = name;

}

… methods (not shown) …

}

What’s wrong?Will this compile?What will happen when you run this?

Tip: To be safe, don’t give the constructor’s parameters the same name as the fields they change, give the parameters a name like “initialName” or “initName”.

Page 39: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

Passing Parameters

Page 40: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 40

Passing Primitive Types

State of Memory

public void myMethod( int one, float two ){ one = 25; two = 35.4f;}

AA

A. A. Local variables do not exist before the method execution

A. A. Local variables do not exist before the method execution

At before myMethodAA

Codex = 10;y = 20;tester.myMethod( x, y );

xx 1010

y 1020

Page 41: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 41

State of Memory

B. B. The values of arguments are copied to the parameters.

B. B. The values of arguments are copied to the parameters.

Values are copied at BB

public void myMethod( int one, float two ){ one = 25; two = 35.4f;}

Codex = 10;y = 20;tester.myMethod( x, y ); BB

xx 1010

y 1020

one 1010

two 1020.0f

Passing Primitive Types

Page 42: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 42

CC

State of Memory

C. C. The values of parameters are

changed.

C. C. The values of parameters are

changed.

After is executedCC

public void myMethod( int one, float two ){ one = 25; two = 35.4f;}

Codex = 10;y = 20;tester.myMethod( x, y );

Passing Primitive Types

xx 1010

y 1020

10one

two 10

25

35.4f

10

20.0f

Page 43: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 43

Code

DD

State of Memory

D. D. Parameters are erased. Arguments remain unchanged.

D. D. Parameters are erased. Arguments remain unchanged.

At after myMethodDD

public void myMethod( int one, float two ){ one = 25; two = 35.4f;}

x = 10;y = 20;tester.myMethod( x, y );

xx 1010

y 1020

Passing Primitive Types

10one

two 10

25

35.4f

Page 44: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 44

Passing Parameters

Arguments are passed to a method using the pass-by-value scheme Parameters and arguments do not have to have

the same name Whether or not they have the same name,

parameters are separate copies of the arguments

Parameters are local to the method, i.e., they only exist while inside the method. Changes made to the parameters will not affect the value of corresponding arguments

* Note: the argument is the variable or value given in the method call, while the parameter is the variable in the method definition that copies that value)

Page 45: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 45

Passing Objects as Parameters

Object-type parameters copy the reference to the passed object calls to methods of the parameter does affect

the original object’s state sometimes known as “pass-by-reference” BUT, if we assign parameter variable to another

object, then original object is not affected anymore

Think of it this way: pass-by-value, but the value being passed is the

pointer to the object, not the object itself

Page 46: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 46

Passing Objects

State of Memory

public void myMethod( BankAccount one, BankAccount two )

{ one.deposit( 50 ); two.withdraw( 10 );}

AA

A. A. Local variables do not exist before the method execution

A. A. Local variables do not exist before the method execution

At before myMethodAA

in BankApplet

BankAccount x = new BankAccount( 100 );

BankAccount y = new BankAccount( 20 );

tester.myMethod( x, y );

xx 10 y 10

BankAccount

100int balance

BankAccount

20int balance

Page 47: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 47

Passing Objects

State of Memory

public void myMethod( BankAccount one, BankAccount two )

{ one.deposit( 50 ); two.withdraw( 10 );}

B. B. Parameters one and two copy the references to the objects pointed to by x and y

B. B. Parameters one and two copy the references to the objects pointed to by x and y

At entering myMethodBB

in BankApplet

BankAccount x = new BankAccount( 100 );

BankAccount y = new BankAccount( 20 );

tester.myMethod( x, y );

xx 10 y 10

BankAccount

100int balance

BankAccount

20int balance

BB

one

two

Page 48: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 48

Passing Objects

State of Memory

public void myMethod( BankAccount one, BankAccount two )

{ one.deposit( 50 ); two.withdraw( 10 );}

C. C. Method calls to one and two affect the same objects referred to by x and y

C. C. Method calls to one and two affect the same objects referred to by x and y

At after running codeCC

in BankApplet

BankAccount x = new BankAccount( 100 );

BankAccount y = new BankAccount( 20 );

tester.myMethod( x, y );

xx 10 y 10

BankAccountint balance

150

BankAccountint balance

10

CC

one 10

two 10

Page 49: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 49

Passing Objects

State of Memory

public void myMethod( BankAccount one, BankAccount two )

{ one.deposit( 50 ); two.withdraw( 10 );}

D. D. When myMethod returns, parameters one and two disappear.But objects x and y have been changed.

D. D. When myMethod returns, parameters one and two disappear.But objects x and y have been changed.

At after running codeDD

in BankApplet

BankAccount x = new BankAccount( 100 );

BankAccount y = new BankAccount( 20 );

tester.myMethod( x, y );

xx 10 y 10

BankAccount

150int balance

BankAccount

10int balance

DD

one

two

Page 50: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 50

Reassigning Parameters

State of Memory

public void myMethod( BankAccount one, BankAccount two )

{ one = two; one.deposit( 50 ); two.withdraw( 10 );}

Suppose we reassign one to something else within the method body

Suppose we reassign one to something else within the method body

At entering myMethodBB

in BankApplet

BankAccount x = new BankAccount( 100 );

BankAccount y = new BankAccount( 20 );

tester.myMethod( x, y );

xx 10 y 10

BankAccount

100int balance

BankAccount

20int balance

BB

one 10

two 10

Page 51: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 51

State of Memory

public void myMethod( BankAccount one, BankAccount two )

{ one = two; one.deposit( 50 ); two.withdraw( 10 );}

C. C. Method calls to one and two affect the same object (same as pointed to by y)x is not affected.

C. C. Method calls to one and two affect the same object (same as pointed to by y)x is not affected.

At after running codeCC

in BankApplet

BankAccount x = new BankAccount( 100 );

BankAccount y = new BankAccount( 20 );

tester.myMethod( x, y );

xx 10 y 10

BankAccount

100int balance

BankAccount

60int balance

CC

one 10

two 10

Reassigning Parameters

Page 52: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 52

Passing Objects

State of Memory

At after running codeDD

in BankApplet

BankAccount x = new BankAccount( 100 );

BankAccount y = new BankAccount( 20 );

tester.myMethod( x, y );

xx 10 y 10

BankAccount

100int balance

BankAccount

60int balance

DD

D. D. When myMethod returns, parameters one and two disappear.Objects y has been changed.

D. D. When myMethod returns, parameters one and two disappear.Objects y has been changed.

public void myMethod( BankAccount one, BankAccount two )

{ one = two; one.deposit( 50 ); two.withdraw( 10 );}

one

two

Page 53: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 53

Passing parameters in passALoad

public void passALoad( double amount,PrepaidAccount acct )

{

if ( amount <= credit )

{

credit -= amount;

acct.loadCredit( amount );

}

}

Page 54: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 54

Passing parameters in passALoad

The passALoad method employs both pass-by-value and pass-by-reference schemes

The amount parameter copies the value of the argument

It does not change nor affect the value of the argument during and after method execution

Page 55: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 55

Passing parameters in passALoad

The acct parameter on the other hand copies the reference to a particular account object

Method calls done through acct affect the actual PrepaidAccount object and change its state

The changes remain after the method is executed

Page 56: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

Strings

Page 57: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 57

Strings In Java, strings are objects and String is a

“built-in” class with special treatment String s = new String( “Hello” ); is the same as

String s = “Hello”; “Hello” is an example of a String literal representing

a String object The + operator can be used for String concatenation

The String class has certain methods length() indexOf(…) substring(…) equals(…) equalsIgnoreCase(…)

Page 58: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 58

String Equality

String s1 = “Bob”;String s2 = “Bo” + “b”;if ( s1 == s2 ){ System.out.println( “Equal.” );}

What does this print out? Why?

Page 59: Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java

6/28/2004Copyright 2004, by the authors of these slides, and Ateneo

de Manila University. All rights reserved L7: Objects

Slide 59

String Equality Recall that since Strings are objects, String

variables are references It’s possible to have 2 String objects with the

same value, but they are different objects Checking for equality of two references

(pointers) checks if they are pointing to the SAME object

If they are not the SAME object, then it will NOT be equal, even if they have the same value

Use this instead:if ( s1.equals( s2 ) ) …