BAEL-6943: Difference between String and StringBuffer in Java (#14815)

* Add files via upload

* Add files via upload

* Add files via upload

* Add files via upload

* Add files via upload

* Add files via upload

* Add files via upload

* Delete core-java-modules/core-java-lang-6/src/main/java/com/baeldung/intposneg directory

* Delete core-java-modules/core-java-lang-6/src/test/java/com/baeldung/intposneg directory

* Add files via upload

* Delete core-java-modules/core-java-numbers-conversions/src/main/java/com/baeldung/stringtoint/intposneg directory

* Add files via upload

* Add files via upload

* Delete core-java-modules/core-java-numbers-conversions/src/main/java/com/baeldung/intposneg directory

* Delete core-java-modules/core-java-numbers-conversions/src/test/java/com/baeldung/intposneg directory

* Add files via upload

* Add files via upload

* Delete core-java-modules/core-java-lang-6/src/main/java/com/baeldung/diffstringstringbuffer directory

* Delete core-java-modules/core-java-lang-6/src/test/java/com/baeldung/diffstringstringbuffer directory

* Delete core-java-modules/core-java-string-apis-2/src/main/java/com/baeldung/diffstringstringbuffer directory

* Delete core-java-modules/core-java-string-apis-2/src/test/java/com/baeldung/diffstringstringbuffer directory

* Add files via upload

* Add files via upload
This commit is contained in:
Paul Greer 2023-11-09 21:52:12 +00:00 committed by GitHub
parent 734d0c2c08
commit 499ed4bbbd
3 changed files with 80 additions and 0 deletions

View File

@ -0,0 +1,33 @@
package com.baeldung.diffstringstringbuffer;
import java.lang.reflect.Field;
import sun.misc.Unsafe;
public class MemoryAddress {
public long getMemoryAddress(Object object) {
Object[] arrayOfObject = new Object[]{object};
Unsafe unsafe = null;
try {
Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
unsafe = (Unsafe) field.get(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
assert unsafe != null;
long arrayBaseOffset = unsafe.arrayBaseOffset(Object[].class);
int addressSize = unsafe.addressSize();
long objectAddress;
if (addressSize == 4) {
objectAddress = unsafe.getInt(arrayOfObject, arrayBaseOffset);
} else {
if (addressSize == 8) {
objectAddress = unsafe.getLong(arrayOfObject, arrayBaseOffset);
} else {
throw new Error("Error: Size not supported: " + addressSize);
}
}
return objectAddress;
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.diffstringstringbuffer;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class StringBufferUnitTest {
@Test
void whenStringBufferVariableIsReassigned_thenVariableRetainsOriginalMemoryAddress() {
MemoryAddress memoryAddress = new MemoryAddress();
StringBuffer stringBuffer = new StringBuffer("DownTown");
long address1 = memoryAddress.getMemoryAddress(stringBuffer);
stringBuffer.insert(0, "Coder");
assertEquals(stringBuffer.toString(), "CoderDownTown");
long address2 = memoryAddress.getMemoryAddress(stringBuffer);
assertEquals(address1, address2);
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.diffstringstringbuffer;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import org.junit.jupiter.api.Test;
public class StringUnitTest {
@Test
void whenStringVariableIsReassigned_thenVariableGetsNewMemoryAddress() {
MemoryAddress memoryAddress = new MemoryAddress();
String s1 = "DownTown";
long address1 = memoryAddress.getMemoryAddress(s1);
s1 = "Coder";
long address2 = memoryAddress.getMemoryAddress(s1);
assertNotEquals(address1, address2);
}
@Test
void whenStringsAreIncludedInAStringBuffer_thenThoseStringsRemainImmutable() {
String s1 = "TopCat";
StringBuffer stringBuffer = new StringBuffer("DownTown");
stringBuffer.append(s1);
stringBuffer.reverse();
assertNotEquals(s1, "taCpoT");
stringBuffer.delete(0, 3);
assertEquals(s1, "TopCat");
}
}