{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

L6-Aliases-Nested classes-Interfaces

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

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

View Full Document Right Arrow Icon
CSE205 Concepts of Computer Science and Data Structures
Image of page 1

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

View Full Document Right Arrow Icon
Outline Reference Nested and Inner Class Interfaces
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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:
Image of page 4
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"
Image of page 5

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

View Full Document Right Arrow Icon
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
Image of page 6
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.
Image of page 7

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

View Full Document Right Arrow Icon
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
Image of page 8
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.
Image of page 9

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

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern