0

Suppose I have the following class:

class Box<T> {
    public Box(T value) {
        this.value = value;
    }
    public T value;
}

Why can’t I do this:

Object obj = new Object();
boolean isBox = obj instanceof Box<Object>;
|  Error:
|  java.lang.Object cannot be safely cast to Box<java.lang.Object>
|  boolean isBox = obj instanceof Box<Object>;
|                  ^-^

But I can do this:

Object obj = new Object();
boolean isBox = obj instanceof Box<?>;
schuelermine
  • 1,958
  • 2
  • 17
  • 31

0 Answers0