일부 사례에서, 특히 루프에서 박싱은 중대한 성능 저하의 원인이 될 수 있습니다.
박싱 연산의 개수를 평가하는 데에는 경험적 지식이 적용됩니다. 예를 들어, 루프 내부의 변환은 훨씬 더 많은 것으로 간주됩니다.
예:
public void example() {
Integer value = 12;
needBox(value);
for (int i = 0; i < 10; i++) {
// Loop usages considered as happening more often
needPrimitive(value);
}
}
void needPrimitive(int value) {}
void needBox(Integer value) {}
빠른 수정을 적용한 후:
public void example() {
int value = 12;
needBox(value);
for (int i = 0; i < 10; i++) {
// 루프 사용은 더 자주 발생하는 것으로 간주됩니다
needPrimitive(value);
}
}
void needPrimitive(int value) {}
void needBox(Integer value) {}
2018.2의 새로운 기능