Java generics SuppressWarnings(“unchecked”) mystery

前端 未结 4 1416
盖世英雄少女心
盖世英雄少女心 2021-02-14 03:28

Why does code alternative(1) compile without warnings, and code alternative(2) produce an \"unchecked cast\" warning?

Common for both:

class Foo         


        
4条回答
  •  面向向阳花
    2021-02-14 03:54

    I'm able to emulate this strange behaviour on my Windows 7 64b machine with:

    • Java(TM) SE Runtime Environment (build 1.7.0_02-b13)
    • OpenJDK Runtime Environment (build 1.8.0-ea-lambda-nightly-h1669-20121030-b63-b00)

    Which means both the OpenJDK and the Oracle JDK are affected, both JDK7 and JDK8 (yes, you can already download it).

    Eclipse, since it uses its own JDT compiler, doesn't have this problem.

    So it seems that this is indeed a javac bug. If you report it, please keep me updated.

    EDIT:

    I've also located a JDK6 installation on my computer, so I tried that one and actually, it works without a warning in both cases, which is the correct behaviour:

    • Java(TM) SE Runtime Environment (build 1.6.0_23-b05)

    Although my Windows are 64b, all the said JDKs are only 32b.

提交回复
热议问题