Introducing Integer Cache

This commit is contained in:
Ali Dehghani 2020-06-02 22:03:54 +04:30
parent f5c5cc5290
commit b07bcce7ef
1 changed files with 11 additions and 0 deletions

View File

@ -26,6 +26,17 @@ public class StringToIntOrIntegerUnitTest {
assertThat(result).isEqualTo(new Integer(42));
}
@Test
public void givenString_whenCallingValueOf_shouldCacheSomeValues() {
for (int i = -128; i <= 127; i++) {
String value = i + "";
Integer first = Integer.valueOf(value);
Integer second = Integer.valueOf(value);
assertThat(first).isSameAs(second);
}
}
@Test
public void givenString_whenCallingIntegerConstructor_shouldConvertToInt() {
String givenString = "42";