SlideShare ist ein Scribd-Unternehmen logo
1 von 70
Shallow vs Deep Copy
MARCUS BIEL,Software Craftsman
www.marcus-biel.com
Copyright © 2016 Marcus Bielwww.marcus-biel.com
What is a Copy in Java?
Shallow Copy Deep Copy
Without fully understanding the idea of “shallow versus deep”
Copyright © 2016 Marcus Bielwww.marcus-biel.com
What is a Copy in Java?
We can already see that the expression is related to a copy.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
What is a Copy in Java?
So let’s start with the simplest question – What actually is a “copy” in Java?
Copyright © 2016 Marcus Bielwww.marcus-biel.com
What is a Copy in Java?
To answer that question, we have to differentiate between a reference copy
and an object copy.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Reference Copy
myCar1
myCar2
Car Object 1
As the name implies, a reference copy creates a copy of a
reference variable pointing to an object.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Reference Copy
myCar1
myCar2
Car Object 1
So we will have two reference variables referencing the same object.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Object Copy
On the other hand, an object copy creates a copy of the object itself.
myCar1
myCar2
Car Object
Car Object Copy
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Object Copy
So as you can see here, we will have two reference variables
that each reference a different object
myCar1
myCar2
Car Object
Car Object Copy
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Object Copy
myCar1
myCar2
Car Object
Car Object Copy
Shallow and Deep Copy are both related to an object copy.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
What is an Object?
Object
But what is an object, actually ?
Often, an object is represented as a single coffee bean.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
What is an Object?
Object
However, that’s just a very simplified view. Let’s look at this in more detail.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
What is an Object?
Say we have a Person object.
Person
Copyright © 2016 Marcus Bielwww.marcus-biel.com
What is an Object?
This Person object is composed of other objects.
Our Person has a Name
Person
Name
Copyright © 2016 Marcus Bielwww.marcus-biel.com
What is an Object?
and an Address.
Person
Name Address
Copyright © 2016 Marcus Bielwww.marcus-biel.com
What is an Object?
The Name object is composed of a FirstName object
Person
Name Address
First
Name
Copyright © 2016 Marcus Bielwww.marcus-biel.com
What is an Object?
and a LastName object.
Person
Name Address
First
Name
Last
Name
Copyright © 2016 Marcus Bielwww.marcus-biel.com
The Address object is composed of a Street object
Person
Name Address
First
Name
Last
Name
Street
Copyright © 2016 Marcus Bielwww.marcus-biel.com
What is an Object?
and a City object. So when we say “Person object”,
Person
Name Address
First
Name
Last
Name
Street City
Copyright © 2016 Marcus Bielwww.marcus-biel.com
What is an Object?
Person
Name Address
First
Name
Last
Name
Street City
We are actually referring to this entire network of objects.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Reason to Copy an Object
So why would we want to make an object copy?
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Reason to Copy an Object
An object copy, also called a clone, is usually created
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Reason to Copy an Object
If we want to modify or move an object, while still preserving the
state of the original object.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Ways to Copy an Object
There are different ways to copy an object, for example by
using a copy constructor or a clone method.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Ways to Copy an Object
In short, I do not recommend that you use the clone
method to copy an object.
For more details, watch my video about the topic.
http://www.marcus-biel.com/object-clone-method/
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Ways to Copy an Object
In this episode, we will use a copy constructor to create
both a Shallow and Deep Copy.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Shallow Copy vs. Deep Copy
Shallow Copy Deep Copy
Now that we have a solid foundation, let me explain the actual difference
between a “Shallow” and a “Deep Copy” of an object.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Shallow Copy
Shallow Copy
Let’s start with a Shallow Copy.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Shallow Copy
Here you can see the object structure of our Person object again.
Person
Name Address
First
Name
Last
Name
Street City
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Shallow Copy
To create a Shallow Copy, we only copy the “main” object, Person.
Name Address
First
Name
Last
Name
Street City
Person Person
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Shallow Copy
So a Shallow Copy references
the same inner objects as the original Person.
Person
Name Address
First
Name
Last
Name
Street City
Person
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Shallow Copy
Therefore,
a Shallow Copy and its source are closely related to each other.
Person
Name Address
First
Name
Last
Name
Street City
Person
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Shallow Copy
As an example,
a change to the Address object will reflect in both Persons.
Person
Name Address
First
Name
Last
Name
Street City
Person
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Shallow Copy
This is a problem if you need two truly independent Person objects.
Person
Name Address
First
Name
Last
Name
Street City
Person
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Deep Copy
Deep Copy
In contrast to a Shallow Copy,
a Deep Copy is a fully independent copy of an object.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Deep Copy
So we start again with our Person object.
Person
Name Address
First
Name
Last
Name
Street City
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Deep Copy
But this time, we copy the entire object structure.
Person
Name Address
First
Name
Last
Name
Street City
Person
Name Address
First
Name
Last
Name
Street City
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Deep Copy
Now both Person objects can be changed independently.
Person
Name Address
First
Name
Last
Name
Street City
Person
Name Address
First
Name
Last
Name
Street City
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Deep Copy
A change in one of the Address objects
would not be reflected in the other Person’s Address.
Person
Name Address
First
Name
Last
Name
Street City
Person
Name Address
First
Name
Last
Name
Street City
Copyright © 2016 Marcus Bielwww.marcus-biel.com
A Mixed Approach
Finally, a combination of a Shallow and a Deep Copy is also possible.
Fields that need to be changed will be deep copies.
Person
Name Address
First
Name
Last
Name
Street City
Person
Address
First
Name
Last
Name
Copyright © 2016 Marcus Bielwww.marcus-biel.com
A Mixed Approach
All other fields will be shared.
Person
Name Address
First
Name
Last
Name
Street City
Person
Address
First
Name
Last
Name
Copyright © 2016 Marcus Bielwww.marcus-biel.com
A Mixed Approach
Okay, I think we’ve had enough of the circles - let’s just see the code!
Person
Name Address
First
Name
Last
Name
Street City
Person
Address
First
Name
Last
Name
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Shallow Copy
Shallow Copy
Just like before, we’ll start with a Shallow Copy.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Shallow Copy
public class Person {
private Name name;
private Address address;
public Person(Person originalPerson) {
this.name = originalPerson.name;
this.address = originalPerson.address;
}
[…]
}
So we have a class Person, which internally uses a Name and an Address.
The copy constructor takes the original Person object and copies
its reference variables.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Shallow Copy
Person mother = new Person(new Name(…), new Address(…));
Okay, now that we have seen the code of the Person class, let’s actually
make use of it. First, we create a Person object, called “mother”, with a
Name and an Address.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Shallow Copy
Person mother = new Person(new Name(…), new Address(…));
[…]
Person son = new Person(mother);
Somewhere later in the code, we will create a copy of this Person object
and assign this copy to the reference variable “son”.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Shallow Copy
Person mother = new Person(new Name(…), new Address(…));
[…]
Person son = new Person(mother);
The Mother object will be taken as a basis for the Son object,
as they will both share the same address.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Shallow Copy
Person mother = new Person(new Name(…), new Address(…));
[…]
Person son = new Person(mother);
[…]
son.moveOut(new Street(…), new City(…));
Much later, when the son has grown old enough to support
himself, he wants to move out - only to find out that his mommy
has joined him.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Shallow Copy
Person mother = new Person(new Name(…), new Address(…));
[…]
Person son = new Person(mother);
[…]
son.moveOut(new Street(…), new City(…));
To better understand what actually happened, let’s revisit the code.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Shallow Copy
Person mother = new Person(new Name(…), new Address(…));
[…]
Person son = new Person(mother);
[…]
son.moveOut(new Street(…), new City(…));
Can you figure out what’s wrong?
Think about it for a moment.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Shallow Copy
Person mother = new Person(new Name(…), new Address(…));
[…]
Person son = new Person(mother);
[…]
son.moveOut(new Street(…), new City(…));
Okay, so, the issue is that in the moveOut method, we directly changed
our internal address object by replacing its Street and City objects.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Shallow Copy
Person mother = new Person(new Name(…), new Address(…));
[…]
Person son = new Person(mother);
[…]
son.moveOut(new Street(…), new City(…));
Because the address object is shared with our mother object,
the change reflected in both Person objects.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Shallow Copy
Person mother = new Person(new Name(…), new Address(…));
[…]
Person son = new Person(mother);
[…]
son.moveOut(new Address(…));
To fix the issue, we could simply assign a new Address object to son.
Just for the record, however, please note that our son object wouldn't be a
true shallow copy anymore.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Shallow Copy
Person mother = new Person(new Name(…), new Address(…));
[…]
Person son = new Person(mother);
[…]
son.moveOut(new Address(…));
In conclusion, if used correctly, a Shallow Copy may be fine,
but it could also lead to unexpected behaviour.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Deep Copy
Deep Copy
Last but not least,
let’s look at the code used to create a Deep Copy of the Person object.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Deep Copy
public class Person {
private Name name;
private Address address;
public Person(Person otherPerson) {
this.name = new Name(otherPerson.name);
this.address = new Address(otherPerson.address);
}
[…]
}
Again, we use a copy constructor,
but this time we will create a Deep Copy of the object.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Deep Copy
public class Person {
private Name name;
private Address address;
public Person(Person otherPerson) {
this.name = new Name(otherPerson.name);
this.address = new Address(otherPerson.address);
}
[…]
}
So we create two instances of Name and Address by using yet more copy
constructors. But actually that’s not the end of the story.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Deep Copy
public class Person {
private Name name;
private Address address;
public Person(Person otherPerson) {
this.name = new Name(otherPerson.name);
this.address = new Address(otherPerson.address);
}
[…]
}
For a true Deep Copy, we have to continue copying all of the object’s
nested elements, until there are only primitive types or so called
“Immutables” left.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Deep Copy
public class Street {
private String name;
private int number;
public Street(Street otherStreet){
this.name = otherStreet.name;
this.number = otherStreet.number;
}
[…]
}
Let’s look at the Street class to better illustrate this. The Street class consists
of two instance variables -
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Deep Copy
public class Street {
private String name;
private int number;
public Street(Street otherStreet){
this.name = otherStreet.name;
this.number = otherStreet.number;
}
[…]
}
…. String name and int number. Let’s look at the int number first:
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Deep Copy
public class Street {
private String name;
private int number;
public Street(Street otherStreet){
this.name = otherStreet.name;
this.number = otherStreet.number;
}
[…]
}
It is a primitive value and not an object. It has no reference variable.
It’s just a simple value that can never be shared.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Deep Copy
public class Street {
private String name;
private int number;
public Street(Street otherStreet){
this.name = otherStreet.name;
this.number = otherStreet.number;
}
[…]
}
So when in the copy constructor we say “this dot number equals
otherStreet dot number”, we are automatically receiving a copy of the
value.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Deep Copy
public class Street {
private String name;
private int number;
public Street(Street otherStreet){
this.name = otherStreet.name;
this.number = otherStreet.number;
}
[…]
}
The other instance variable, “name” is an object of type String.
However, String is an Immutable.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Deep Copy
public class Street {
private String name;
private int number;
public Street(Street otherStreet){
this.name = otherStreet.name;
this.number = otherStreet.number;
}
[…]
}
In short, an Immutable is an object, that,
once created, can never be changed again.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Deep Copy
public class Street {
private String name;
private int number;
public Street(Street otherStreet){
this.name = otherStreet.name;
this.number = otherStreet.number;
}
[…]
}
Therefore, you can share it without having to create a Deep Copy of it.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Deep Copy
Person mother = new Person(new Name(…), new Address(…));
[…]
Person son = new Person(mother);
[…]
son.moveOut(new Street(…), new City(…));
Now that we’re using a Deep Copy,
the son can successfully move out! Hooray!
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Deep Copy
Person mother = new Person(new Name(…), new Address(…));
[…]
Person son = new Person(mother);
[…]
son.moveOut(new Street(…), new City(…));
However, as a final note of caution, please note that just because a Deep
Copy lets you get away with this, don’t infer that I suggest you do so.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Deep Copy
Person mother = new Person(new Name(…), new Address(…));
[…]
Person son = new Person(mother);
[…]
son.moveOut(new Street(…), new City(…));
Directly changing the internal details of
an object is bad code style and should generally be avoided.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Deep Copy
Person mother = new Person(new Name(…), new Address(…));
[…]
Person son = new Person(mother);
[…]
son.moveOut(new Address(…));
In our example,
I would recommend using a new Address object instead.
Copyright © 2016 Marcus Bielwww.marcus-biel.com
Thank You
Copyright © 2016
Marcus Biel
All rights reserved

Weitere ähnliche Inhalte

Mehr von Marcus Biel

Mehr von Marcus Biel (7)

Immutable Classes in Java
Immutable Classes in JavaImmutable Classes in Java
Immutable Classes in Java
 
Hash code and equals
Hash code and equalsHash code and equals
Hash code and equals
 
Java Collections Framework Inroduction with Video Tutorial
Java Collections Framework Inroduction with Video TutorialJava Collections Framework Inroduction with Video Tutorial
Java Collections Framework Inroduction with Video Tutorial
 
Java Comparable Interface Video Tutorial
Java Comparable Interface Video Tutorial Java Comparable Interface Video Tutorial
Java Comparable Interface Video Tutorial
 
Java ArrayList Video Tutorial
Java ArrayList Video TutorialJava ArrayList Video Tutorial
Java ArrayList Video Tutorial
 
LinkedList vs Arraylist- an in depth look at java.util.LinkedList
LinkedList vs Arraylist- an in depth look at java.util.LinkedListLinkedList vs Arraylist- an in depth look at java.util.LinkedList
LinkedList vs Arraylist- an in depth look at java.util.LinkedList
 
Linked List data structure
Linked List data structureLinked List data structure
Linked List data structure
 

Kürzlich hochgeladen

reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdfreStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
Ken Fuller
 
一比一定(购)堪培拉大学毕业证(UC毕业证)成绩单学位证
一比一定(购)堪培拉大学毕业证(UC毕业证)成绩单学位证一比一定(购)堪培拉大学毕业证(UC毕业证)成绩单学位证
一比一定(购)堪培拉大学毕业证(UC毕业证)成绩单学位证
eqaqen
 
Top profile Call Girls In Varanasi [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Varanasi [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Varanasi [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Varanasi [ 7014168258 ] Call Me For Genuine Models ...
gajnagarg
 
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
nirzagarg
 
Top profile Call Girls In Gangtok [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In Gangtok [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In Gangtok [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In Gangtok [ 7014168258 ] Call Me For Genuine Models W...
gajnagarg
 
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
yynod
 
Top profile Call Girls In Agartala [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Agartala [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Agartala [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Agartala [ 7014168258 ] Call Me For Genuine Models ...
gajnagarg
 
Top profile Call Girls In Ratnagiri [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Ratnagiri [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Ratnagiri [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Ratnagiri [ 7014168258 ] Call Me For Genuine Models...
gajnagarg
 
一比一定(购)南昆士兰大学毕业证(USQ毕业证)成绩单学位证
一比一定(购)南昆士兰大学毕业证(USQ毕业证)成绩单学位证一比一定(购)南昆士兰大学毕业证(USQ毕业证)成绩单学位证
一比一定(购)南昆士兰大学毕业证(USQ毕业证)成绩单学位证
eqaqen
 
Top profile Call Girls In chittoor [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In chittoor [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In chittoor [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In chittoor [ 7014168258 ] Call Me For Genuine Models ...
gajnagarg
 
怎样办理宾夕法尼亚大学毕业证(UPenn毕业证书)成绩单学校原版复制
怎样办理宾夕法尼亚大学毕业证(UPenn毕业证书)成绩单学校原版复制怎样办理宾夕法尼亚大学毕业证(UPenn毕业证书)成绩单学校原版复制
怎样办理宾夕法尼亚大学毕业证(UPenn毕业证书)成绩单学校原版复制
yynod
 
<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...
<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...
<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...
gynedubai
 
Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...
gajnagarg
 
Top profile Call Girls In Shivamogga [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Shivamogga [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Shivamogga [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Shivamogga [ 7014168258 ] Call Me For Genuine Model...
nirzagarg
 

Kürzlich hochgeladen (20)

reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdfreStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
reStartEvents 5:9 DC metro & Beyond V-Career Fair Employer Directory.pdf
 
一比一定(购)堪培拉大学毕业证(UC毕业证)成绩单学位证
一比一定(购)堪培拉大学毕业证(UC毕业证)成绩单学位证一比一定(购)堪培拉大学毕业证(UC毕业证)成绩单学位证
一比一定(购)堪培拉大学毕业证(UC毕业证)成绩单学位证
 
Top profile Call Girls In Varanasi [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Varanasi [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Varanasi [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Varanasi [ 7014168258 ] Call Me For Genuine Models ...
 
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Etawah [ 7014168258 ] Call Me For Genuine Models We...
 
Top profile Call Girls In Gangtok [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In Gangtok [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In Gangtok [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In Gangtok [ 7014168258 ] Call Me For Genuine Models W...
 
Mysore Escorts Service Girl ^ 9332606886, WhatsApp Anytime Mysore
Mysore Escorts Service Girl ^ 9332606886, WhatsApp Anytime MysoreMysore Escorts Service Girl ^ 9332606886, WhatsApp Anytime Mysore
Mysore Escorts Service Girl ^ 9332606886, WhatsApp Anytime Mysore
 
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
怎样办理哥伦比亚大学毕业证(Columbia毕业证书)成绩单学校原版复制
 
Top profile Call Girls In Agartala [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Agartala [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Agartala [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Agartala [ 7014168258 ] Call Me For Genuine Models ...
 
Top profile Call Girls In Ratnagiri [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Ratnagiri [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Ratnagiri [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Ratnagiri [ 7014168258 ] Call Me For Genuine Models...
 
Complete Curriculum Vita for Paul Warshauer
Complete Curriculum Vita for Paul WarshauerComplete Curriculum Vita for Paul Warshauer
Complete Curriculum Vita for Paul Warshauer
 
一比一定(购)南昆士兰大学毕业证(USQ毕业证)成绩单学位证
一比一定(购)南昆士兰大学毕业证(USQ毕业证)成绩单学位证一比一定(购)南昆士兰大学毕业证(USQ毕业证)成绩单学位证
一比一定(购)南昆士兰大学毕业证(USQ毕业证)成绩单学位证
 
Top profile Call Girls In chittoor [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In chittoor [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In chittoor [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In chittoor [ 7014168258 ] Call Me For Genuine Models ...
 
怎样办理宾夕法尼亚大学毕业证(UPenn毕业证书)成绩单学校原版复制
怎样办理宾夕法尼亚大学毕业证(UPenn毕业证书)成绩单学校原版复制怎样办理宾夕法尼亚大学毕业证(UPenn毕业证书)成绩单学校原版复制
怎样办理宾夕法尼亚大学毕业证(UPenn毕业证书)成绩单学校原版复制
 
Personal Brand Exploration - Fernando Negron
Personal Brand Exploration - Fernando NegronPersonal Brand Exploration - Fernando Negron
Personal Brand Exploration - Fernando Negron
 
Guide to a Winning Interview May 2024 for MCWN
Guide to a Winning Interview May 2024 for MCWNGuide to a Winning Interview May 2024 for MCWN
Guide to a Winning Interview May 2024 for MCWN
 
Vip Malegaon Escorts Service Girl ^ 9332606886, WhatsApp Anytime Malegaon
Vip Malegaon Escorts Service Girl ^ 9332606886, WhatsApp Anytime MalegaonVip Malegaon Escorts Service Girl ^ 9332606886, WhatsApp Anytime Malegaon
Vip Malegaon Escorts Service Girl ^ 9332606886, WhatsApp Anytime Malegaon
 
<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...
<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...
<DUBAI>Abortion pills IN UAE {{+971561686603*^Mifepristone & Misoprostol in D...
 
Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...
 
Launch Your Research Career: A Beginner's Guide
Launch Your Research Career: A Beginner's GuideLaunch Your Research Career: A Beginner's Guide
Launch Your Research Career: A Beginner's Guide
 
Top profile Call Girls In Shivamogga [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Shivamogga [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Shivamogga [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Shivamogga [ 7014168258 ] Call Me For Genuine Model...
 

Shallow vs. Deep Copy with funny animations

  • 1. Shallow vs Deep Copy MARCUS BIEL,Software Craftsman www.marcus-biel.com
  • 2. Copyright © 2016 Marcus Bielwww.marcus-biel.com What is a Copy in Java? Shallow Copy Deep Copy Without fully understanding the idea of “shallow versus deep”
  • 3. Copyright © 2016 Marcus Bielwww.marcus-biel.com What is a Copy in Java? We can already see that the expression is related to a copy.
  • 4. Copyright © 2016 Marcus Bielwww.marcus-biel.com What is a Copy in Java? So let’s start with the simplest question – What actually is a “copy” in Java?
  • 5. Copyright © 2016 Marcus Bielwww.marcus-biel.com What is a Copy in Java? To answer that question, we have to differentiate between a reference copy and an object copy.
  • 6. Copyright © 2016 Marcus Bielwww.marcus-biel.com Reference Copy myCar1 myCar2 Car Object 1 As the name implies, a reference copy creates a copy of a reference variable pointing to an object.
  • 7. Copyright © 2016 Marcus Bielwww.marcus-biel.com Reference Copy myCar1 myCar2 Car Object 1 So we will have two reference variables referencing the same object.
  • 8. Copyright © 2016 Marcus Bielwww.marcus-biel.com Object Copy On the other hand, an object copy creates a copy of the object itself. myCar1 myCar2 Car Object Car Object Copy
  • 9. Copyright © 2016 Marcus Bielwww.marcus-biel.com Object Copy So as you can see here, we will have two reference variables that each reference a different object myCar1 myCar2 Car Object Car Object Copy
  • 10. Copyright © 2016 Marcus Bielwww.marcus-biel.com Object Copy myCar1 myCar2 Car Object Car Object Copy Shallow and Deep Copy are both related to an object copy.
  • 11. Copyright © 2016 Marcus Bielwww.marcus-biel.com What is an Object? Object But what is an object, actually ? Often, an object is represented as a single coffee bean.
  • 12. Copyright © 2016 Marcus Bielwww.marcus-biel.com What is an Object? Object However, that’s just a very simplified view. Let’s look at this in more detail.
  • 13. Copyright © 2016 Marcus Bielwww.marcus-biel.com What is an Object? Say we have a Person object. Person
  • 14. Copyright © 2016 Marcus Bielwww.marcus-biel.com What is an Object? This Person object is composed of other objects. Our Person has a Name Person Name
  • 15. Copyright © 2016 Marcus Bielwww.marcus-biel.com What is an Object? and an Address. Person Name Address
  • 16. Copyright © 2016 Marcus Bielwww.marcus-biel.com What is an Object? The Name object is composed of a FirstName object Person Name Address First Name
  • 17. Copyright © 2016 Marcus Bielwww.marcus-biel.com What is an Object? and a LastName object. Person Name Address First Name Last Name
  • 18. Copyright © 2016 Marcus Bielwww.marcus-biel.com The Address object is composed of a Street object Person Name Address First Name Last Name Street
  • 19. Copyright © 2016 Marcus Bielwww.marcus-biel.com What is an Object? and a City object. So when we say “Person object”, Person Name Address First Name Last Name Street City
  • 20. Copyright © 2016 Marcus Bielwww.marcus-biel.com What is an Object? Person Name Address First Name Last Name Street City We are actually referring to this entire network of objects.
  • 21. Copyright © 2016 Marcus Bielwww.marcus-biel.com Reason to Copy an Object So why would we want to make an object copy?
  • 22. Copyright © 2016 Marcus Bielwww.marcus-biel.com Reason to Copy an Object An object copy, also called a clone, is usually created
  • 23. Copyright © 2016 Marcus Bielwww.marcus-biel.com Reason to Copy an Object If we want to modify or move an object, while still preserving the state of the original object.
  • 24. Copyright © 2016 Marcus Bielwww.marcus-biel.com Ways to Copy an Object There are different ways to copy an object, for example by using a copy constructor or a clone method.
  • 25. Copyright © 2016 Marcus Bielwww.marcus-biel.com Ways to Copy an Object In short, I do not recommend that you use the clone method to copy an object. For more details, watch my video about the topic. http://www.marcus-biel.com/object-clone-method/
  • 26. Copyright © 2016 Marcus Bielwww.marcus-biel.com Ways to Copy an Object In this episode, we will use a copy constructor to create both a Shallow and Deep Copy.
  • 27. Copyright © 2016 Marcus Bielwww.marcus-biel.com Shallow Copy vs. Deep Copy Shallow Copy Deep Copy Now that we have a solid foundation, let me explain the actual difference between a “Shallow” and a “Deep Copy” of an object.
  • 28. Copyright © 2016 Marcus Bielwww.marcus-biel.com Shallow Copy Shallow Copy Let’s start with a Shallow Copy.
  • 29. Copyright © 2016 Marcus Bielwww.marcus-biel.com Shallow Copy Here you can see the object structure of our Person object again. Person Name Address First Name Last Name Street City
  • 30. Copyright © 2016 Marcus Bielwww.marcus-biel.com Shallow Copy To create a Shallow Copy, we only copy the “main” object, Person. Name Address First Name Last Name Street City Person Person
  • 31. Copyright © 2016 Marcus Bielwww.marcus-biel.com Shallow Copy So a Shallow Copy references the same inner objects as the original Person. Person Name Address First Name Last Name Street City Person
  • 32. Copyright © 2016 Marcus Bielwww.marcus-biel.com Shallow Copy Therefore, a Shallow Copy and its source are closely related to each other. Person Name Address First Name Last Name Street City Person
  • 33. Copyright © 2016 Marcus Bielwww.marcus-biel.com Shallow Copy As an example, a change to the Address object will reflect in both Persons. Person Name Address First Name Last Name Street City Person
  • 34. Copyright © 2016 Marcus Bielwww.marcus-biel.com Shallow Copy This is a problem if you need two truly independent Person objects. Person Name Address First Name Last Name Street City Person
  • 35. Copyright © 2016 Marcus Bielwww.marcus-biel.com Deep Copy Deep Copy In contrast to a Shallow Copy, a Deep Copy is a fully independent copy of an object.
  • 36. Copyright © 2016 Marcus Bielwww.marcus-biel.com Deep Copy So we start again with our Person object. Person Name Address First Name Last Name Street City
  • 37. Copyright © 2016 Marcus Bielwww.marcus-biel.com Deep Copy But this time, we copy the entire object structure. Person Name Address First Name Last Name Street City Person Name Address First Name Last Name Street City
  • 38. Copyright © 2016 Marcus Bielwww.marcus-biel.com Deep Copy Now both Person objects can be changed independently. Person Name Address First Name Last Name Street City Person Name Address First Name Last Name Street City
  • 39. Copyright © 2016 Marcus Bielwww.marcus-biel.com Deep Copy A change in one of the Address objects would not be reflected in the other Person’s Address. Person Name Address First Name Last Name Street City Person Name Address First Name Last Name Street City
  • 40. Copyright © 2016 Marcus Bielwww.marcus-biel.com A Mixed Approach Finally, a combination of a Shallow and a Deep Copy is also possible. Fields that need to be changed will be deep copies. Person Name Address First Name Last Name Street City Person Address First Name Last Name
  • 41. Copyright © 2016 Marcus Bielwww.marcus-biel.com A Mixed Approach All other fields will be shared. Person Name Address First Name Last Name Street City Person Address First Name Last Name
  • 42. Copyright © 2016 Marcus Bielwww.marcus-biel.com A Mixed Approach Okay, I think we’ve had enough of the circles - let’s just see the code! Person Name Address First Name Last Name Street City Person Address First Name Last Name
  • 43. Copyright © 2016 Marcus Bielwww.marcus-biel.com Shallow Copy Shallow Copy Just like before, we’ll start with a Shallow Copy.
  • 44. Copyright © 2016 Marcus Bielwww.marcus-biel.com Shallow Copy public class Person { private Name name; private Address address; public Person(Person originalPerson) { this.name = originalPerson.name; this.address = originalPerson.address; } […] } So we have a class Person, which internally uses a Name and an Address. The copy constructor takes the original Person object and copies its reference variables.
  • 45. Copyright © 2016 Marcus Bielwww.marcus-biel.com Shallow Copy Person mother = new Person(new Name(…), new Address(…)); Okay, now that we have seen the code of the Person class, let’s actually make use of it. First, we create a Person object, called “mother”, with a Name and an Address.
  • 46. Copyright © 2016 Marcus Bielwww.marcus-biel.com Shallow Copy Person mother = new Person(new Name(…), new Address(…)); […] Person son = new Person(mother); Somewhere later in the code, we will create a copy of this Person object and assign this copy to the reference variable “son”.
  • 47. Copyright © 2016 Marcus Bielwww.marcus-biel.com Shallow Copy Person mother = new Person(new Name(…), new Address(…)); […] Person son = new Person(mother); The Mother object will be taken as a basis for the Son object, as they will both share the same address.
  • 48. Copyright © 2016 Marcus Bielwww.marcus-biel.com Shallow Copy Person mother = new Person(new Name(…), new Address(…)); […] Person son = new Person(mother); […] son.moveOut(new Street(…), new City(…)); Much later, when the son has grown old enough to support himself, he wants to move out - only to find out that his mommy has joined him.
  • 49. Copyright © 2016 Marcus Bielwww.marcus-biel.com Shallow Copy Person mother = new Person(new Name(…), new Address(…)); […] Person son = new Person(mother); […] son.moveOut(new Street(…), new City(…)); To better understand what actually happened, let’s revisit the code.
  • 50. Copyright © 2016 Marcus Bielwww.marcus-biel.com Shallow Copy Person mother = new Person(new Name(…), new Address(…)); […] Person son = new Person(mother); […] son.moveOut(new Street(…), new City(…)); Can you figure out what’s wrong? Think about it for a moment.
  • 51. Copyright © 2016 Marcus Bielwww.marcus-biel.com Shallow Copy Person mother = new Person(new Name(…), new Address(…)); […] Person son = new Person(mother); […] son.moveOut(new Street(…), new City(…)); Okay, so, the issue is that in the moveOut method, we directly changed our internal address object by replacing its Street and City objects.
  • 52. Copyright © 2016 Marcus Bielwww.marcus-biel.com Shallow Copy Person mother = new Person(new Name(…), new Address(…)); […] Person son = new Person(mother); […] son.moveOut(new Street(…), new City(…)); Because the address object is shared with our mother object, the change reflected in both Person objects.
  • 53. Copyright © 2016 Marcus Bielwww.marcus-biel.com Shallow Copy Person mother = new Person(new Name(…), new Address(…)); […] Person son = new Person(mother); […] son.moveOut(new Address(…)); To fix the issue, we could simply assign a new Address object to son. Just for the record, however, please note that our son object wouldn't be a true shallow copy anymore.
  • 54. Copyright © 2016 Marcus Bielwww.marcus-biel.com Shallow Copy Person mother = new Person(new Name(…), new Address(…)); […] Person son = new Person(mother); […] son.moveOut(new Address(…)); In conclusion, if used correctly, a Shallow Copy may be fine, but it could also lead to unexpected behaviour.
  • 55. Copyright © 2016 Marcus Bielwww.marcus-biel.com Deep Copy Deep Copy Last but not least, let’s look at the code used to create a Deep Copy of the Person object.
  • 56. Copyright © 2016 Marcus Bielwww.marcus-biel.com Deep Copy public class Person { private Name name; private Address address; public Person(Person otherPerson) { this.name = new Name(otherPerson.name); this.address = new Address(otherPerson.address); } […] } Again, we use a copy constructor, but this time we will create a Deep Copy of the object.
  • 57. Copyright © 2016 Marcus Bielwww.marcus-biel.com Deep Copy public class Person { private Name name; private Address address; public Person(Person otherPerson) { this.name = new Name(otherPerson.name); this.address = new Address(otherPerson.address); } […] } So we create two instances of Name and Address by using yet more copy constructors. But actually that’s not the end of the story.
  • 58. Copyright © 2016 Marcus Bielwww.marcus-biel.com Deep Copy public class Person { private Name name; private Address address; public Person(Person otherPerson) { this.name = new Name(otherPerson.name); this.address = new Address(otherPerson.address); } […] } For a true Deep Copy, we have to continue copying all of the object’s nested elements, until there are only primitive types or so called “Immutables” left.
  • 59. Copyright © 2016 Marcus Bielwww.marcus-biel.com Deep Copy public class Street { private String name; private int number; public Street(Street otherStreet){ this.name = otherStreet.name; this.number = otherStreet.number; } […] } Let’s look at the Street class to better illustrate this. The Street class consists of two instance variables -
  • 60. Copyright © 2016 Marcus Bielwww.marcus-biel.com Deep Copy public class Street { private String name; private int number; public Street(Street otherStreet){ this.name = otherStreet.name; this.number = otherStreet.number; } […] } …. String name and int number. Let’s look at the int number first:
  • 61. Copyright © 2016 Marcus Bielwww.marcus-biel.com Deep Copy public class Street { private String name; private int number; public Street(Street otherStreet){ this.name = otherStreet.name; this.number = otherStreet.number; } […] } It is a primitive value and not an object. It has no reference variable. It’s just a simple value that can never be shared.
  • 62. Copyright © 2016 Marcus Bielwww.marcus-biel.com Deep Copy public class Street { private String name; private int number; public Street(Street otherStreet){ this.name = otherStreet.name; this.number = otherStreet.number; } […] } So when in the copy constructor we say “this dot number equals otherStreet dot number”, we are automatically receiving a copy of the value.
  • 63. Copyright © 2016 Marcus Bielwww.marcus-biel.com Deep Copy public class Street { private String name; private int number; public Street(Street otherStreet){ this.name = otherStreet.name; this.number = otherStreet.number; } […] } The other instance variable, “name” is an object of type String. However, String is an Immutable.
  • 64. Copyright © 2016 Marcus Bielwww.marcus-biel.com Deep Copy public class Street { private String name; private int number; public Street(Street otherStreet){ this.name = otherStreet.name; this.number = otherStreet.number; } […] } In short, an Immutable is an object, that, once created, can never be changed again.
  • 65. Copyright © 2016 Marcus Bielwww.marcus-biel.com Deep Copy public class Street { private String name; private int number; public Street(Street otherStreet){ this.name = otherStreet.name; this.number = otherStreet.number; } […] } Therefore, you can share it without having to create a Deep Copy of it.
  • 66. Copyright © 2016 Marcus Bielwww.marcus-biel.com Deep Copy Person mother = new Person(new Name(…), new Address(…)); […] Person son = new Person(mother); […] son.moveOut(new Street(…), new City(…)); Now that we’re using a Deep Copy, the son can successfully move out! Hooray!
  • 67. Copyright © 2016 Marcus Bielwww.marcus-biel.com Deep Copy Person mother = new Person(new Name(…), new Address(…)); […] Person son = new Person(mother); […] son.moveOut(new Street(…), new City(…)); However, as a final note of caution, please note that just because a Deep Copy lets you get away with this, don’t infer that I suggest you do so.
  • 68. Copyright © 2016 Marcus Bielwww.marcus-biel.com Deep Copy Person mother = new Person(new Name(…), new Address(…)); […] Person son = new Person(mother); […] son.moveOut(new Street(…), new City(…)); Directly changing the internal details of an object is bad code style and should generally be avoided.
  • 69. Copyright © 2016 Marcus Bielwww.marcus-biel.com Deep Copy Person mother = new Person(new Name(…), new Address(…)); […] Person son = new Person(mother); […] son.moveOut(new Address(…)); In our example, I would recommend using a new Address object instead.
  • 70. Copyright © 2016 Marcus Bielwww.marcus-biel.com Thank You Copyright © 2016 Marcus Biel All rights reserved