소정(@iamsojung) 님이 질문해주셨던 힙 오염의 의미 참고 문서 첨부합니다! https://en.wikipedia.org/wiki/Heap_pollution > In the [Java programming](https://en.wikipedia.org/wiki/Java_programming) language, [heap](https://en.wikipedia.org/wiki/Memory_management#Dynamic_memory_allocation) pollution is a situation that arises when a variable of a [parameterized type](https://en.wikipedia.org/wiki/Java_syntax#Generics) refers to an object that is not of that parameterized type.[[1]](https://en.wikipedia.org/wiki/Heap_pollution#cite_note-Java-1) This situation is normally detected during [compilation](https://en.wikipedia.org/wiki/Compile_time) and indicated with an [unchecked warning](https://en.wikipedia.org/wiki/Type_system#Static_type_checking).[[1]](https://en.wikipedia.org/wiki/Heap_pollution#cite_note-Java-1) Later, during [runtime](https://en.wikipedia.org/wiki/Runtime_(program_lifecycle_phase)) heap pollution will often cause a [ClassCast](https://en.wikipedia.org/wiki/Type_conversion)[Exception](https://en.wikipedia.org/wiki/Exception_handling).[[2]](https://en.wikipedia.org/wiki/Heap_pollution#cite_note-Langer-2) 런타임에 힙에 생성되는 인스턴스 타입이 컴파일타임의 타입과 다를 수 있는 상황에 Unchecked Warning 과 ClassCastException 이 발생할 가능성이 있다면 힙 오염이라고 표현한다고 합니다. ```java /** * https://en.wikipedia.org/wiki/Heap_pollution */ public class HeapPollutionDemo { public static void main(String[] args) { Set s = new TreeSet<Integer>(); Set<String> ss = s; // unchecked warning s.add(new Integer(42)); // another unchecked warning Iterator<String> iter = ss.iterator(); while (iter.hasNext()) { String str = iter.next(); // ClassCastException thrown System.out.println(str); } } } ```
Activity