添加 Java 中的 String Pool 简介 内容

This commit is contained in:
2022-06-20 17:26:15 -04:00
parent de42646567
commit b7d0d45f28
2 changed files with 16 additions and 9 deletions
@@ -3,37 +3,44 @@ package com.ossez.stringpool;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class StringPoolUnitTest {
private static Logger logger = LoggerFactory.getLogger(StringPoolUnitTest.class);
@Test
public void whenCreatingConstantStrings_thenTheirAddressesAreEqual() {
String constantString1 = "Baeldung";
String constantString2 = "Baeldung";
String constantString1 = "HoneyMoose";
String constantString2 = "HoneyMoose";
assertThat(constantString1).isSameAs(constantString2);
}
@Test
public void whenCreatingStringsWithTheNewOperator_thenTheirAddressesAreDifferent() {
String newString1 = new String("Baeldung");
String newString2 = new String("Baeldung");
String newString1 = new String("HoneyMoose");
String newString2 = new String("HoneyMoose");
assertThat(newString1).isNotSameAs(newString2);
logger.info("newString1 Address: {}", System.identityHashCode(newString1));
logger.info("newString2 Address: {}", System.identityHashCode(newString2));
}
@Test
public void whenComparingConstantAndNewStrings_thenTheirAddressesAreDifferent() {
String constantString = "Baeldung";
String newString = new String("Baeldung");
String constantString = "HoneyMoose";
String newString = new String("HoneyMoose");
assertThat(constantString).isNotSameAs(newString);
}
@Test
public void whenInterningAStringWithIdenticalValueToAnother_thenTheirAddressesAreEqual() {
String constantString = "interned Baeldung";
String newString = new String("interned Baeldung");
String constantString = "interned HoneyMoose";
String newString = new String("interned HoneyMoose");
assertThat(constantString).isNotSameAs(newString);