String ossId1 = new String("yunjh.1216");
String ossId2 = new String("yunjh.1216");
System.out.println(ossId1 == ossId2); // false
## 정적 팩토리 메서드 사용
- #1 : 불변 클래스의 정적 팩토리 메서드
- 불변 클래스의 생성자는 자바 9에서 deprecated API 로 지정 되었음
~~~java
Boolean isJungHyeOssId = Boolean.valueOf("yunjh.1217");
/**
* Returns a {@code Boolean} instance representing the specified
* {@code boolean} value. If the specified {@code boolean} value
* is {@code true}, this method returns {@code Boolean.TRUE};
* if it is {@code false}, this method returns {@code Boolean.FALSE}.
* If a new {@code Boolean} instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Boolean(boolean)}, as this method is likely to yield
* significantly better space and time performance.
*
* @param b a boolean value.
* @return a {@code Boolean} instance representing {@code b}.
* @since 1.4
*/
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
/**
* The {@code Boolean} object corresponding to the primitive
* value {@code true}.
*/
public static final Boolean TRUE = new Boolean(true);
/**
* The {@code Boolean} object corresponding to the primitive
* value {@code false}.
*/
public static final Boolean FALSE = new Boolean(false);
생성 비용이 값비싼 객체를 재사용 (ex> java.util.regex.Pattern)
정규표현식용 Pattern 인스턴스를 클래스 초기화 과정에서 직접 생성 후 재사용함.
// AS-IS
private static final String BYLINE_REPORTER_REGEX = "[가-힣]{2,4} ?기자";
- 지연 초기화(#83)
- removeByline 메서드가 호출될 때 필드를 초기화함 (권하지 않음)
- 지연 초기화는 코드를 복잡하게 만드는데, 성능은 크게 개선되지 않을 때가 많다. (#67)
## 어탭터
- 어댑터는 인터페이스를 통해 뒤에 있는 객체로 연결해주는 view여서 여러개 만들 필요가 없다.
- 같은 인스턴스를 대변하는 여러 개의 인스턴스 생성은 피하자
~~~java
Map<String, Object> map = new HashMap<>();
map.put("Effictive", "Java");
Set<String> set1 = map.keySet();
Set<String> set2 = map.keySet();
assertThat(set1).isSameAs(set2); // TRUE
set1.remove("Java");
System.out.println(set1.size()); // 1
System.out.println(set1.size()); // 1
Map 인터페이스의 keySet 메서드는 Map 객체 안의 키 전부를 담은 Set 인터페이스의 뷰를 반환한다.
동일한 Map에서 호출하는 keySet 은 같은 Map을 대변하기 때문에 keySet이 뷰 객체를 여러개 만들 필요도 없고 이득도 없다.
오토박싱(auto boxing)
오토박싱은 기본 타입과 그에 대응하는 박싱된 기본 타입의 구분을 흐려주시만 성능에서는 그렇지 않다. (#61)
Long sum = 0L; <= 느려짐
박싱된 기본 타입보다는 기본 타입으로 사용하고, 의도치 않은 오토박싱이 숨어들지 않도록 주의
코드 6-3 끔찍이 느리다! 객체가 만들어지는 위치를 찾았는가?
private static long sum() {
Long sum = 0L;
for (long i = 0 ; i <= Integer.MAX_VALUE ; i++) {
sum += i;
}
return sum;
}
객체의 재사용
17 : 불변 객체는 언제든 재사용 가능
String ossId1 = new String("yunjh.1216"); String ossId2 = new String("yunjh.1216"); System.out.println(ossId1 == ossId2); // false
생성 비용이 값비싼 객체를 재사용 (ex> java.util.regex.Pattern)
private void removeByline(List sentences) {
sentences.set(lastIndex, sentences.get(lastIndex).matches(BYLINE_EMAIL_REGEX).trim());
}
// TO-BE
private static final String BYLINE_REPORTER_REGEX = "[가-힣]{2,4} ?기자";
private static final Pattern BYLINE_REPORTER_PATTERN = Pattern.compile(BYLINE_REPORTER_REGEX, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
private void removeByline(List sentences) {
sentences.set(lastIndex, BYLINE_REPORTER_PATTERN.matcher(sentences.get(lastIndex)).matches().trim());
}
오토박싱(auto boxing)
박싱된 기본 타입보다는 기본 타입으로 사용하고, 의도치 않은 오토박싱이 숨어들지 않도록 주의
코드 6-3 끔찍이 느리다! 객체가 만들어지는 위치를 찾았는가?