File tree Expand file tree Collapse file tree
core-java/src/test/java/com/baeldung/stringpool Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .stringpool ;
2+
3+ import org .junit .Test ;
4+ import static org .assertj .core .api .Assertions .*;
5+
6+ public class StringPoolUnitTest {
7+
8+ @ Test
9+ public void whenCreatingConstantStrings_thenTheirAddressesAreEqual () {
10+ String constantString1 = "Baeldung" ;
11+ String constantString2 = "Baeldung" ;
12+
13+ assertThat (constantString1 ).isSameAs (constantString2 );
14+ }
15+
16+ @ Test
17+ public void whenCreatingStringsWithTheNewOperator_thenTheirAddressesAreDifferent () {
18+ String newString1 = new String ("Baeldung" );
19+ String newString2 = new String ("Baeldung" );
20+
21+ assertThat (newString1 ).isNotSameAs (newString2 );
22+ }
23+
24+ @ Test
25+ public void whenComparingConstantAndNewStrings_thenTheirAddressesAreDifferent () {
26+ String constantString = "Baeldung" ;
27+ String newString = new String ("Baeldung" );
28+
29+ assertThat (constantString ).isNotSameAs (newString );
30+ }
31+
32+ @ Test
33+ public void whenInterningAStringWithIdenticalValueToAnother_thenTheirAddressesAreEqual () {
34+ String constantString = "interned Baeldung" ;
35+ String newString = new String ("interned Baeldung" );
36+
37+ assertThat (constantString ).isNotSameAs (newString );
38+
39+ String internedString = newString .intern ();
40+
41+ assertThat (constantString ).isSameAs (internedString );
42+ }
43+ }
You can’t perform that action at this time.
0 commit comments