Added code for Mutable vs Immutable objects in java

This commit is contained in:
Imran Alam 2024-02-03 21:43:12 +05:30
parent 7391417ce8
commit a1fb6eb66b
8 changed files with 105 additions and 69 deletions

View File

@ -1,23 +0,0 @@
package com.baeldung.objectcopy;
import java.io.*;
class DeepCopyPerson implements Serializable {
int age;
public DeepCopyPerson(int age) {
this.age = age;
}
public static DeepCopyPerson deepCopy(DeepCopyPerson person) throws IOException, ClassNotFoundException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(person);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bis);
return (DeepCopyPerson) in.readObject();
}
}

View File

@ -1,10 +0,0 @@
package com.baeldung.objectcopy;
class ShallowCopyPerson {
int age;
public ShallowCopyPerson(int age) {
this.age = age;
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.objectmutability;
public final class ImmutablePerson {
private final String name;
private final int age;
public ImmutablePerson(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}

View File

@ -1,19 +0,0 @@
package com.baeldung.objectcopy;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class DeepCopyPersonUnitTest {
@Test
public void givenPerson_whenDeepCopy_thenIndependentCopy() throws IOException, ClassNotFoundException {
DeepCopyPerson person1 = new DeepCopyPerson(30);
DeepCopyPerson person2 = DeepCopyPerson.deepCopy(person1);
person1.age = 25;
assertEquals(30, person2.age);
}
}

View File

@ -1,17 +0,0 @@
package com.baeldung.objectcopy;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ShallowCopyPersonUnitTest {
@Test
public void givenPerson_whenShallowCopy_thenSharedReference() {
ShallowCopyPerson person1 = new ShallowCopyPerson(30);
ShallowCopyPerson person2 = person1;
person1.age = 25;
assertEquals(25, person2.age);
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.objectmutability;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
public class ImmutableObjectExamplesUnitTest {
@Test
public void givenImmutableString_whenConcat_thenNotSameAndCorrectValues() {
String originalString = "Hello";
String modifiedString = originalString.concat(" World");
assertNotSame(originalString, modifiedString);
assertEquals("Hello", originalString);
assertEquals("Hello World", modifiedString);
}
@Test
public void givenImmutableInteger_whenAdd_thenNotSameAndCorrectValue() {
Integer immutableInt = 42;
Integer modifiedInt = immutableInt + 8;
assertNotSame(immutableInt, modifiedInt);
assertEquals(42, (int) immutableInt);
assertEquals(50, (int) modifiedInt);
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.objectmutability;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ImmutablePersonUnitTest {
@Test
public void givenImmutablePerson_whenModifyName_thenCompilationError() {
ImmutablePerson person = new ImmutablePerson("John", 30);
// person.setName("Jane");
}
@Test
public void givenImmutablePerson_whenAccessFields_thenCorrectValues() {
ImmutablePerson person = new ImmutablePerson("John", 30);
assertEquals("John", person.getName());
assertEquals(30, person.getAge());
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.objectmutability;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class MutableObjectExamplesUnitTest {
@Test
public void givenMutableString_whenModify_thenCorrectValue() {
StringBuilder mutableString = new StringBuilder("Hello");
mutableString.append(" World");
assertEquals("Hello World", mutableString.toString());
}
@Test
public void givenMutableList_whenAddElement_thenCorrectSize() {
List<String> mutableList = new ArrayList<>();
mutableList.add("Java");
assertEquals(1, mutableList.size());
}
}