Skip to content

[item29] νž™ μ˜€μ—Όμ˜ μ •ν™•ν•œ 의미 #47

@jun108059

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);
        }
    }
}

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