javaunchecked-cast

Why doesn't the following codes cause "unchecked cast" warning?


I think that (String)x is an unchecked cast, but the compiler does not give any warning. Why does it happen?

public static void main(String[] args) {
        Object x=new Object();
        String y=(String)x;
    }

Solution

  • I think that (String)x is an unchecked cast

    No, it's not. It's checked at execution time - if the cast isn't valid, it will throw an exception.

    Unchecked casts are about casts that look like they'll do checking, but actually don't check everything you'd expect, due to type erasure. For example:

    List<String> strings = new ArrayList<>();
    Object mystery = strings;
    List<Integer> integers = (List<Integer>) mystery;
    integers.add(0); // Works
    String x = strings.get(0); // Bang! Implicit cast fails
    

    Here the cast of (List<Integer>) mystery only checks that the object mystery refers to is a List - not a List<Integer>. The Integer part is not checked, because there's no such concept as a List<Integer> at execution time.

    So in our example, that cast succeeds where it wouldn't with "real" checking - and the add call works fine, because that's just populating an Object[] with an Integer element. The last line fails because the call to get() implicitly performs a cast.

    As far as the VM is concerned, the example code is effectively:

    List strings = new ArrayList();
    Object mystery = strings;
    List integers = (List) mystery;
    integers.add(0);
    String x = (String) strings.get(0);