Presentation is loading. Please wait.

Presentation is loading. Please wait.

References Revisted (Ch 5)

Similar presentations


Presentation on theme: "References Revisted (Ch 5)"— Presentation transcript:

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 The definition of the variable is what puts aside memory for it. Until this happens, you can't refer to it. This is where you get the "undefined variable" type of syntax error. (Point out that, even though you define an attribute like this in a class definition, that does not actually put aside memory for the variable. Memory is not allocated for an object until the object is instantiated from the class. This slide actually applies to variables that are not attributes of objects.) The amount of memory put aside depends on the type of the variable. For an int, it is 32 bits. For a String, it depends on how many characters are in it (String is a class rather than a primitive type, so it has behaviour that includes making itself big enough to hold the characters it has to store). The name of the variable is the symbol we use to refer to that memory location. We don't have to know where it is. Whenever we say anAge in our program, the computer goes to the memory location it has attached that label to, and retrieves the value in there. 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. The default constructor for the Person class gives the initial values of "" to the name and 0 to the age. If we used the other constructor to create the object, there would be different values in the attribute areas of memory. Show the two constructors in the BlueJ editor.

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. At the moment, it is not the address of any Person object. It is just big enough to hold such an address. Explain what an address is. 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. New person object aPerson From now on, whenever we refer to aPerson in our code, the computer looks in that memory location, finds the address of another memory location, and goes there to get the value you are talking about. Give an example of what the memory address might be, and what the contents of the aPerson location would therefore be. Draw a picture of this happening. This is what happens when you create an instance of a class rather than just a variable of a primitive type such as int, double, boolean. A String variable is also a reference variable, since String is a class. We should, in fact, create a String variable by saying String aName = new String(); but Java has included a short cut that makes it look as if Strings are just primitive data types, because we use them so often.

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; 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". Draw a picture on the overhead showing how the memory for the parameter comes into existence at the time the method is called, and disappears again when the method terminates. There will be more on scoping later.

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. Draw a picture of changing the value of the parameter, while the myAge variable remains unchanged. 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); } Point out where you would have to put the name of an object if the methods were in different classes. Explain what is happening here with all the printlns. Run it in BlueJ.

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 " } Bring up BlueJ, and create a TestPerson object. Run its testIntPassing method and show the output in the Java terminal window. Explain that the name of the formal parameter and the actual argument don't have to be the same, because the called method sees only the value anyway. If they are the same, that's fine too. Mention that this technique of printing out values of things to see what is happening is a common one when trying to debug a program. We will avoid looking at the debugger at this stage, as I think it would only confuse most students.

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. Memory for object in calling method Reference to object in calling method object in called method Compare the reference to the object to the key to a house. The method is passed the key, not the house. If you file a bit off the key so it fits a different lock, it might open another house door but not the original one. The value of the variable is passed by value. For an object, the value of the variable is actually the address of the object. The called method gets a copy of this.

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 " } Draw the picture of the object with the attributes in its memory area. The one we will look at here is the name of the Person object.

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 " } Explain the code, line by line. Put some emphasis on the series of returned values in the call. Run the testObjectPassing method in BlueJ, and show the output on the Java terminal window.


Download ppt "References Revisted (Ch 5)"

Similar presentations


Ads by Google