1 / 8

CSC 143

CSC 143. Making a copy of an object: shallow and deep copy. Example. public class Car { private String make; private double weight; public Car(String theMake , double theWeight ) { make = theMake ; weight = theWeight ; } } Car c = new Car(“Ford”, 2000);

Download Presentation

CSC 143

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 143 Making a copy of an object:shallow and deep copy

  2. Example public class Car {private String make;privatedouble weight;public Car(String theMake, doubletheWeight) { make = theMake; weight = theWeight; }} • Car c = new Car(“Ford”, 2000); • Object diagram • How to make a copy of c? String c “Ford” Car make weight 2000

  3. An alias • Car c = new Car(“Ford”, 2000); • Car alias = c; • Object diagram • Not really a copy! String c alias “Ford” Car make weight 2000

  4. A shallow copy • Car c = new Car(“Ford”, 2000); • Car shallow = ??; // See code later • Object diagram String c “Ford” Car The original Car object is copied exactly as it appears. All of the fields have the same values in the copy as in the original. This is why the “make” field in the copy contains the same address as in the original. A shallow copy shares all of the objects with the original beyond the first level in the object diagram make shallow Car weight 2000 make weight 2000

  5. A deep copy • Car c = new Car(“Ford”, 2000); • Car deep = ??; // See code later • Object diagram: String String deep c “Ford” “Ford” Car Car make make weight weight 2000 2000 Original and copy are totally distinct

  6. Coding public class Car {private String make;privatedouble weight;// many ways // a copy constructor // an instance method // a factory method (static method) } Java also offers the clone method (but clunky!) publicCar(Car c) { //shallowthis.weight = c.weight; this.make = c.make; } publicCar(Car c) { // deepthis.weight = c.weight; this.make = new String(c.make); } publicCar shallowCopy() { return new Car(this.weight, this.make); } publicCar deepCopy() { return new Car(this.weight, new String(this.make)); } public staticCar shallowCopy(Car c) { return new Car(c.weight, c.make); } public static Car deepCopy(Car c) { return new Car(c.weight, new String(c.make)); }

  7. How to choose? • A deep copy requires more execution time and memory • Always return a deep copy if you want to be sure that the original data can’t be changed by a caller. • Sometimes a shallow copy has the same effect as a deep copy since some Java classes are immutable.

  8. Immutable classes • Immutable class: a class whose instances CANNOT be modified once created. • The class doesn’t offer any method that would allow a client to modify an instance of the class. • The String class is immutable • String s = new String(“abc”);String r = s.toUpperCase(); // doesn’t modify s, but creates a new String whose content is “ABC” • Color, wrapper classes (Double, Character, etc.), File, Font are other examples of immutable classes (there are more). And of course you can write your own immutable classes. • In our Car example, the shallow copy behaves like a deep copy (meaning that the original can’t be modified even when shallow copied). String String s r “abc” “ABC”

More Related