What is SuppressWarnings (“unchecked”) in Java?

前端 未结 11 809
轮回少年
轮回少年 2020-11-22 12:48

Sometime when looking through code, I see many methods specify an annotation:

@SuppressWarnings(\"unchecked\")

What does this mean?

11条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 13:34

    It could also mean that the current Java type system version isn't good enough for your case. There were several JSR propositions / hacks to fix this: Type tokens, Super Type Tokens, Class.cast().

    If you really need this supression, narrow it down as much as possible (e.g. don't put it onto the class itself or onto a long method). An example:

    public List getALegacyListReversed() {
       @SuppressWarnings("unchecked") List list =
           (List)legacyLibrary.getStringList();
    
       Collections.reverse(list);
       return list;
    }
    

提交回复
热议问题