Skip to content

[item29] 힙 오염의 정확한 의미 #47

Description

@jun108059

소정(@iamsojung) 님이 질문해주셨던 힙 오염의 의미 참고 문서 첨부합니다!

https://en.wikipedia.org/wiki/Heap_pollution

In the Java programming language, heap pollution is a situation that arises when a variable of a parameterized type refers to an object that is not of that parameterized type.[1] This situation is normally detected during compilation and indicated with an unchecked warning.[1] Later, during runtime heap pollution will often cause a ClassCastException.[2]

런타임에 힙에 생성되는 인스턴스 타입이 컴파일타임의 타입과 다를 수 있는 상황에 Unchecked Warning 과 ClassCastException 이 발생할 가능성이 있다면 힙 오염이라고 표현한다고 합니다.

/**
 * 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions