DOC PREVIEW
UMD CMSC 131 - Lecture 19: Issues in Copying

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)Lecture 19:Issues in CopyingLast time:1.Intro to arrays2.Copying arrays and making arrays bigger3.Array lengths and out-of-bounds indexing4.Passing arrays and array elements to a functionThis lecture set:1.Privacy Leaks2.Different levels of copyCMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)1Foo object Privacy Leaks (continued)public class MutableThing {…public void mutateMe() {…};}public class Foo {private MutableThing q = new MutableThing();…public MutableThing getQ(){return q;}}Consider following codeFoo f = new Foo ();MutableThing m = f.getQ();m.mutateMe();After this executes, what happens?This phenomenon is called a privacy leakPrivate instance variables can be modified outside classBehavior is due to aliasingHeapStackfmMutableThingobjectCMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)2Fixing Privacy LeaksReturn copies of objects referenced by instance variablesTo fix getQ method in Foo:MutableThing getQ(){return new MutableThing(q);}This returns a copy of qChanges made to this copy will not affect originalHeapStackfmFoo object MutableThingobjectMutableThingobjectCMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)3Reference CopyingPerson[] d = {new Person(“SGH”, …),new Person(“Shakira”, …)};Person[] e = d;HeapStackdSGHShakiraeCMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)4Shallow CopyingPerson[] d = {new Person(“SGH”, …),new Person(“Shakira”, …)};Person[] e = new Person[d.length];for (int i=0; i < d.length, i++){e[i] = d[i];}HeapStackdSGHShakiraeCMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)5Deep CopyingPerson[] d = {new Person(“SGH”, …),new Person(“Shakira”, …)};Person[] e = new Person[d.length];for (int i=0; i<d.length; i++) {e[i] = new Person(d[i]);}HeapStackdSGHShakiraeSGHShakiraCMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)6Three Ways of CopyingCDCollector contains an array of CD’s;ReCDCollector contains an array of rewritableCD’s;Reference copypublic ReCD[] getCDsReferenceCopy() {return myFavorites;}Shallow copypublic ReCD[] getCDsShallowCopy() {ReCD[] copy = new ReCD[myFavorites.length];for (int i = 0; i < copy.length; i++)copy[i] = myFavorites[i];return copy;}Deep copypublic ReCD[] getCDsDeepCopy() {ReCD[] copy = new ReCD[myFavorites.length];for (int i = 0; i < copy.length; i++)copy[i] = new ReCD(myFavorites[i]);return copy;}ReCDCollectionOwner p = new RECD…;ReCD[] a = p.getCD…();a[0] = otherCDalreadycreated;a[0].rewrite(“other”,”name”);CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)7When To Use What Kind of Copying?Reference copying is usually a bad idea (not always but realize what you are doing)Deep copying provides maximal protection against aliasing (but takes a lot of time and space if it was not necessary)Storage space and time usedReference: leastShallow: middleDeep: mostIf the class is mutable, aliasing is something to be avoided and you must have true copies to prevent privacy leaks and modifications outside.If you know the class is immutable, aliasing doesn’t hurt but neither does making true copies (except wasted space and time).If storage is an issue, aliasing problems may be worth coping with but must be well


View Full Document

UMD CMSC 131 - Lecture 19: Issues in Copying

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture 19: Issues in Copying
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 19: Issues in Copying and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 19: Issues in Copying 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?