Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
    }
like image 808
Zhu Li Avatar asked May 07 '15 12:05

Zhu Li


2 Answers

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);
like image 85
Jon Skeet Avatar answered Sep 19 '22 15:09

Jon Skeet


Java compiler gives unchecked cast warnings only for generic types

like image 44
Evgeniy Dorofeev Avatar answered Sep 18 '22 15:09

Evgeniy Dorofeev