Creating shallow copy vs deep copy in Java
This commit is contained in:
parent
6cf8a16999
commit
73c2c6bc67
|
@ -0,0 +1,58 @@
|
|||
package com.baeldung.shallowvsdeepcopy;
|
||||
|
||||
class Address implements Cloneable{
|
||||
|
||||
private String streetName;
|
||||
private String zipCode;
|
||||
private String cityName;
|
||||
private String country;
|
||||
|
||||
public Address(String streetName, String zipCode, String cityName, String country) {
|
||||
this.streetName = streetName;
|
||||
this.zipCode = zipCode;
|
||||
this.cityName = cityName;
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getStreetName() {
|
||||
return streetName;
|
||||
}
|
||||
|
||||
public void setStreetName(String streetName) {
|
||||
this.streetName = streetName;
|
||||
}
|
||||
|
||||
public String getZipCode() {
|
||||
return zipCode;
|
||||
}
|
||||
|
||||
public void setZipCode(String zipCode) {
|
||||
this.zipCode = zipCode;
|
||||
}
|
||||
|
||||
public String getCityName() {
|
||||
return cityName;
|
||||
}
|
||||
|
||||
public void setCityName(String cityName) {
|
||||
this.cityName = cityName;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Address clone() {
|
||||
try {
|
||||
Address clone = (Address) super.clone();
|
||||
return clone;
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.baeldung.shallowvsdeepcopy;
|
||||
|
||||
public class UserWithDeepClone implements Cloneable{
|
||||
|
||||
private String name;
|
||||
private Address address;
|
||||
private int age;
|
||||
|
||||
public UserWithDeepClone(String name, Address address, int age) {
|
||||
this.name = name;
|
||||
this.address = address;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserWithDeepClone clone() {
|
||||
try {
|
||||
UserWithDeepClone clone = (UserWithDeepClone) super.clone();
|
||||
clone.setAddress(clone.getAddress().clone());
|
||||
return clone;
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung.shallowvsdeepcopy;
|
||||
|
||||
public class UserWithShallowClone implements Cloneable{
|
||||
|
||||
private String name;
|
||||
private Address address;
|
||||
private int age;
|
||||
|
||||
public UserWithShallowClone(String name, Address address, int age) {
|
||||
this.name = name;
|
||||
this.address = address;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserWithShallowClone clone() {
|
||||
try {
|
||||
UserWithShallowClone clone = (UserWithShallowClone) super.clone();
|
||||
return clone;
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.shallowvsdeepcopy;
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class UserCloneUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenUser_WhenCopyCreated_thenCopyIsShallow(){
|
||||
Address address = new Address("abc", "444-0000", "pqr", "USA");
|
||||
UserWithShallowClone user = new UserWithShallowClone("baeldung", address,32);
|
||||
|
||||
UserWithShallowClone userClone = (UserWithShallowClone)user.clone();
|
||||
assertThat(userClone.getAddress()).isEqualTo(user.getAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserWithDeepCloneMethod_WhenCopyCreated_thenCopyIsDeep(){
|
||||
Address address = new Address("abc", "444-0000", "pqr", "USA");
|
||||
UserWithDeepClone user = new UserWithDeepClone("baeldung", address,32);
|
||||
UserWithDeepClone userClone = (UserWithDeepClone)user.clone();
|
||||
assertThat(userClone.getAddress()).isNotEqualTo(user.getAddress());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue