전체 글
-
Effective Java 아이템 32 (Generic, Varargs, Heap Pollution)Java, JVM 2021. 8. 23. 22:14
힙 오염이란? 힙 오염은 parameterized type의 변수가 자신과 다른 타입의 객체를 참조하는 경우에 발생한다. (Parameterized type은 List, Set 같은 것을 의미한다.) 다음과 같은 상황을 들 수 있다. static method heapPollution() { List stringList = new ArrayList(); List integerList = List.of(1); stringList = (List) (Object) integerList; // 힙 오염 발생 String s = stringList.get(0); // ClassCastException } List 타입인 stringList가 List 타입의 객체를 참조하게 되면서 힙 오염이 발생하였다. 이 코드는 ..