References Revisted Ch 5 - PowerPoint PPT Presentation

About This Presentation
Title:

References Revisted Ch 5

Description:

Memory is allocated to the formal parameter at the time the method is called. ... In the calling method, showing the name of the object before and after the call: ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 14
Provided by: rebecc2
Learn more at: https://www.cs.unca.edu
Category:

less

Transcript and Presenter's Notes

Title: References Revisted Ch 5


1
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

2
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.
3
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.

4
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
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.

6
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.

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
7
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".

8
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.

25
25
myAge
newAge
Memory belonging to some other method
Memory belonging to setAge
9
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)

10
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)

11
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.

12
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())

13
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())
Write a Comment
User Comments (0)
About PowerShow.com