[Java] 'κΉμ 볡μ¬'μ 'μμ 볡μ¬'λ?
μ€λ μ 리ν λ΄μ©μ μλ°μμμ κΉμ λ³΅μ¬ μ μμ 볡μ¬μ΄λ€.
λ°°μ΄μ΄λ κ°μ²΄λ₯Ό 볡μ¬ν΄μ μ¬μ©νλ κ²½μ°κ° λ°μν μ μλλ°, μ΄λ JAVAμμ κΉμ 볡μ¬μ μμ 볡μ¬μ λν΄ λ°°μ μλ€.
μ΄ μ°¨μ΄μ μ λͺ νν ν΄μΌ ν·κ°λ¦¬μ§ μκΈ° λλ¬Έμ μ 리ν΄λκ³ μ νλ€.
Deep Copy vs Shallow Copy
κΉμλ³΅μ¬ (Deep Copy) : 'μ€μ κ°'μ μλ‘μ΄ λ©λͺ¨λ¦¬ 곡κ°μ 볡μ¬νλ€. (ν μμ)
μμλ³΅μ¬ (Shallow Copy) : 'μ£Όμ κ°'μ 볡μ¬νλ€. (μ€νμμ)
μ¬μ€μ, μ΄λ κ²λ§ μ€λͺ νκ² λλ©΄.. μ΄ν΄λ₯Ό λͺ»νλ κ²½μ°κ° λ§λ€.
λ°λΌμ μλ₯Ό λ€μ΄ μ€λͺ ν΄λ³΄μ
μμ볡μ¬(Shallow Copy)
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
//Getters and setters
}
public class ShallowCopy {
public static void main(String[] args) {
Person person1 = new Person("John", 26);
Person person2 = person1; //μμ 볡μ¬
//λ³κ²½ μ
System.out.println("person1: " + person1.getName() + ", " + person1.getAge();
System.out.println("person2: " + person2.getName() + ", " + person2.getAge();
//person1μ λ³κ²½
person1.setName("Liz");
person1.setAge(24);
//λ³κ²½ ν
System.out.println("person1: " + person1.getName() + ", " + person1.getAge());
System.out.println("person2: " + person2.getName() + ", " + person2.getAge());
}
}
//μΆλ ₯ κ²°κ³Ό
person1 : John, 26
person2 : John, 26
person1 : Liz , 24
person2 : Liz, 24
μ¦, μμ 볡μ¬λ κ°μ²΄μ μ£Όμλ§μ 볡μ¬νλ―λ‘ person1κ³Ό person2κ° λμΌν κ°μ²΄λ₯Ό μ°Έμ‘°νλ€λ κ²μ΄λ€.
λ°λΌμ, person1μ λ΄μ©λ§ λ³κ²½νμμλ person2λ κ°μ΄ λ³κ²½λλ€λ κ²μ λ³Ό μ μλ€.
κΉμ λ³΅μ¬ (Deep Copy)
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
//copy μμ±μ for deep copy
public Person(Person other) {
this.name = other.name;
this.age = other.age;
}
//Getters and setters
}
public class DeepCopy {
public static void main(String[] args) {
Person person1 = new Person("chung", 25);
Person person2 = new Person(person1); //κΉμ 볡μ¬
//λ³κ²½ μ
System.out.println("person1: " + person1.getName() + ", " + person1.getAge());
System.out.println("person2: " + person2.getName() + ", " + person2.getAge());
//Person 1μ λ³κ²½
person1.setName("Liz");
person1.setAge(28);
//λ³κ²½ ν
System.out.println("person1: " + person1.getName() + ", " + person1.getAge());
System.out.println("person2: " + person2.getName() + ", " + person2.getAge());
}
}
//κ²°κ³Ό μΆλ ₯
person1 : chung, 25
person2 : chung, 25
person1 : Liz, 28
person2 : chung, 25
μ¦, κΉμ 볡μ¬λ κ°μ²΄μ μ€μ λ΄μ©μ μλ‘μ΄ λ©λͺ¨λ¦¬ 곡κ°μ 볡μ¬νκ² λλ€.
λ°λΌμ person1μ λ΄μ©μ λ³κ²½νλ©΄ person1μ λ΄μ©λ§ λ³κ²½λ λΏ person2λ μν₯μ λ°μ§ μλλ€.
μμ 볡μ¬, κΉμ λ³΅μ¬ μ ννκΈ°
κ·Έλ λ€λ©΄, μμ 볡μ¬μ κΉμ 볡μ¬λ₯Ό μ΄λ€ μν©μ μ΄λ»κ² μ¬μ©ν΄μΌ ν κΉ?
κ°μ²΄ 볡μ¬μ λν μ νμ μ£Όμ΄μ§ μν©μ λ°λΌ κ΅μ₯ν λ¬λΌμ§ μ μλ€.
μμ 볡μ¬λ μ±λ₯μ μ΄μ μ΄ μμ§λ§, λ΄λΆ μνμ λ³κ²½μμ λΆνμν λ³κ²½μ μ΄λν μ μκΈ° λλ¬Έμ μ£Όμν΄μ μ¬μ©ν΄μΌ νλ€. λν κ°μ²΄μ νλκ° κΈ°λ³Έ λ°μ΄ν° νμ μ΄κ±°λ λΆλ³ κ°μ²΄μΌ λλ λ¬Έμ κ° λμ§ μμ§λ§, μ°Έμ‘° λ°μ΄ν° νμ μ κ°μ§ κ°μ²΄μ κ²½μ°μλ μλ³Έ κ°μ²΄μ 볡μ¬λ³Έμ΄ λμΌν μ°Έμ‘°λ₯Ό κ°μ§λ―λ‘ μμμΉ λͺ»ν κ²°κ³Όλ₯Ό μ΄λν μ μμ΄ μ£Όμν΄μΌ νλ€.
λ°λ©΄ κΉμ 볡μ¬λ μμ μ±κ³Ό μμΈ‘ κ°λ₯μ±μ μ κ³΅ν΄ μ£Όμ§λ§, μ±λ₯ μ νλ₯Ό μ΄λν μ μλ€.κ°μ²΄κ° ν¬κ³ 볡μ‘ν κ²½μ°, λͺ¨λ λ΄λΆ κ°μ²΄λ₯Ό 볡μ¬νλ κ²μ΄ ν° μμΈμΌ μ μλ€. λ°λΌμ λ³κ²½ λΆκ°λ₯ν κ°μ²΄λ₯Ό μ¬μ©νκ±°λ λΆλ³ κ°μ²΄μ λ΄λΆ μνλ₯Ό λ³κ²½νλ λ°©μλ±μΌλ‘ κ³ λ €ν΄λ³Ό νμκ° μλ€.
μ¦, κ°μ²΄λ₯Ό 볡μ¬νμ¬ λ€λ₯Έ κ°μ²΄μ μ λ¬ν λμ μλ³Έ κ°μ²΄μ μμ μ μμΉ μλ κ²½μ° "κΉμ볡μ¬"λ₯Ό μ¬μ©νλ©°,
κ°μ²΄μ μΌλΆ μμ±λ§ 볡μ¬νκ³ λλ¨Έμ§λ 곡μ νλ κ²½μ° "μμ 볡μ¬"λ₯Ό μ¬μ©μ κ³ λ €ν΄μΌ νλ€.