Title: References Revisted Ch 5
1References 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
2Creating 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.
3Creating 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.
4Naming 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
5Reference 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.
6Variables 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.
int number 0
0
number
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 obj
Person fred new Person()
fred
7Memory 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".
8Passing 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.
25
25
myAge
newAge
Memory belonging to some other method
Memory belonging to setAge
9Passing 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) -
10Changing 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)
-
11Passing 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. -
12Passing 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())
-
-
13Changing 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())
-