references revisted (ch 5)

13
References Revisted (Ch 5) • Topics: – The difference between reference variables and variables that store primative data types – The difference between reference parameters and parameters that store primative data types

Upload: kalila

Post on 06-Jan-2016

23 views

Category:

Documents


0 download

DESCRIPTION

References Revisted (Ch 5). Topics: The difference between reference variables and variables that store primative data types The difference between reference parameters and parameters that store primative data types. Creating a Variable---A Review. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: References Revisted (Ch 5)

References Revisted (Ch 5)

• Topics:– The difference between reference variables and

variables that store primative data types– The difference between reference parameters

and parameters that store primative data types

Page 2: References Revisted (Ch 5)

Creating a Variable---A Review• We create (or define) a variable by

declaring its type and its name:

–int anAge;

–String aName;

anAge

aName

A variable must be defined before it can be used.

Page 3: References Revisted (Ch 5)

Creating a New Object---A Review

• When we instantiate an object from a class, we use the new operator.

– new Person();

• allocates a block of memory big enough to hold a Person object, and calls the constructor to initialise the values of its attributes. The new operator returns the address of the beginning of that block of memory.

Page 4: References Revisted (Ch 5)

Naming a New Object---A Review

• Usually, we want to give an object a name so that we can refer to it afterwards.

– Person aPerson;

• puts aside enough memory to hold the address of a Person object, and calls it aPerson.

aPerson

Page 5: References Revisted (Ch 5)

Reference Variables---A Review

– Person aPerson = new Person();

• copies the address of the newly created Person object into the memory location called aPerson.

• We say that aPerson is a reference to the Person object. It is a reference variable.

aPerson

New person object

Page 6: References Revisted (Ch 5)

Variables of Primitive Types vs. Reference Variables

• A primitive type is one of the built-in basic data types, e.g. int, double, boolean. The name of the variable is a memory location that stores a value.

number

int number = 0;

A reference variable is an object (e.g. a String). The name of the variable is a memory location that stores the address of the object (i.e. the values of its attributes).

Person fred = new Person(); fred

Person obj

0

Page 7: References Revisted (Ch 5)

Memory Allocation for Parameters

• Memory is allocated to the formal parameter at the time the method is called.

• The memory location is initialised to the actual argument (i.e., the input value).

• The memory for the formal parameter is deallocated when the method finishes executing. We call this "going out of scope".

Page 8: References Revisted (Ch 5)

Passing Basic Data Types as Parameters

• If a parameter is of a basic data type, when the formal parameter comes into existence it is a copy of the actual argument.

• Any change made to the formal parameter in the method will not affect the value in the caller's memory area. This kind of parameter passing is known as call by value.

Memory belonging to setAge

newAgemyAge

Memory belonging to some other method

Page 9: References Revisted (Ch 5)

Passing an int to a Method• To simplify the code, both calling and called methods are in

the same class here. If they are in different classes, you need to ask an object to invoke the method, but otherwise it is the same.

• The calling method, showing the int before and after the call:

public void testIntPassing(){

int number = 50; System.out.println(); System.out.println("int before call is " + number); changeNumber(number); System.out.println("int after call is " + number);

}

Page 10: References Revisted (Ch 5)

Changing the Value of the int

• The called method, showing the int before and after it is changed by this method:

private void changeNumber(int aNumber)

{

System.out.println("int at start of method changeNumber is "

+ aNumber);

aNumber = aNumber + 5;

System.out.println("int at end of method changeNumber is "

+ aNumber);

}

Page 11: References Revisted (Ch 5)

Passing Objects as Parameters

• When an object is passed as a parameter, what is passed to the method is a copy of the reference to the object. A change to the state of the formal parameter produces a change in the state of the actual argument.

Memory for objectin calling method

Reference toobject in calling method

Reference toobject in called method

Page 12: References Revisted (Ch 5)

Passing an Object to a Method• In the calling method, showing the name of the

object before and after the call:

public void testObjectPassing(){ Person friend = new Person("Fred", 20, false);

System.out.println(); System.out.println ("Object attribute before call is "

+ friend.getName()); changeState(friend); System.out.println("Object attribute after call is "

+ friend.getName());}

Page 13: References Revisted (Ch 5)

Changing the State of the Formal Parameter

• In the called method, showing the name of the formal parameter object at the start and the end (because the formal parameter is an object, it has to be asked to do things):

private void changeState(Person someone){

System.out.println("Object attribute at start " + "of method changeState is + someone.getName()); someone.setName(someone.getName().replace('d', 't'));

System.out.println("Object attribute at end " + "of method changeState is " + someone.getName()); }