L6-Aliases-Nested classes-Interfaces

L6-Aliases-Nested classes-Interfaces - CSE205 Concepts of...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSE205 Concepts of Computer Science and Data Structures Outline Reference Nested and Inner Class Interfaces 1-3 Object References Note that a primitive variable contains the value itself, but an object variable contains the address of the object An object reference can be thought of as a pointer to the location of the object Rather than dealing with arbitrary addresses, we often depict a reference graphically "Steve Jobs" name1 num1 38 1-4 Assignment Revisited The act of assignment takes a copy of a value and stores it in a variable For primitive types num1 38 num2 96 Before: num2 = num1; num1 38 num2 38 After: 1-5 Reference Assignment For object references, assignment copies the address name2 = name1; name1 name2 Before: "Steve Jobs" "Steve Wozniak" name1 name2 After: "Steve Jobs" 1-6 Aliases Two or more references that refer to the same object are called aliases of each other That creates an interesting situation: one object can be accessed using multiple reference variables Aliases can be useful, but should be managed carefully Changing an object through one reference changes it for all of its aliases, because there is really only one object Aliases Example When two objects share a same address, we say that they are aliases of each other. Example: Student student1 = new Student(); Student student2 = new Student(); student1 = student2; // making them aliases of each other When we make an assignment such as above for two objects, the address of student2 is copied to the address of student1, and they become aliases. After this assignment, if we make any value change to instance data of one object, the change is also seen in the other object that is an alias of the object that we made a change. So if we do: student2.setLastName(Smith); Then the object student1 will also have the last name Smith. student1 student2 address1 address2 before After becoming aliases. 1-8 Garbage Collection When an object no longer has any valid references to it, it can no longer be accessed by the program The object is useless, and therefore is called garbage Java performs automatic garbage collection periodically, returning an object's memory to the system for future use In other languages, the programmer is responsible for performing garbage collection When an object (not primitive data) is passed as a parameter for a method, say method1, its address is passed. Thus if we make any value changes to instance data of the object in method1, those changes will be reflected in the object in the method from where method1 was called. Example: public class ParameterPassing { public static void main (String args) { ParameterTester tester = new ParameterTester(); int a1 = 111; Num a2 = new Num (222); Num a3 = new Num (333); tester.changeValues (a1, a2, a3); } } public class ParameterTester { public void changeValues (int f1, Num f2, Num f3) { f1 = 999; f2.setValue(888); f3 = new Num (777); } } Passing objects as parameters...
View Full Document

This note was uploaded on 04/18/2011 for the course CSE 205 taught by Professor Matasu during the Spring '08 term at ASU.

Page1 / 32

L6-Aliases-Nested classes-Interfaces - CSE205 Concepts of...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online