8

I know that we cannot invoke instanceof List<E> because List<E> is not a reifiable type. Both instanceof List and instanceof List<?> work; however the eclipse IDE suggests use instanceof List<?>.

I wonder why it suggests unbound wildcard instanceof List<?> instead of raw call instanceof List. Does the unbound wildcard instanceof List<?> have any advantage over the raw call instanceof List?

Thank in advance.

Edit 1: In effect, instanceof List and instanceof List<?> is same as the compiler will erasure the type when compiling. But beside the cosmetic reason as Mena points out, does it have any other reason to use instanceof List<?> in favor of instanceof List?

Edit 2: According this entry from Oracle:

  1. The type of an instanceof/cast expression is raw

This happens very frequently, as javac forbids instanceof expressions whose target type is a generic type; for casts, the compiler is slightly more permissive since casts to generic type are allowed but a warning is issued (see above). Anyway, the raw type should be replaced by an unbounded wildcard, as they have similar properties w.r.t. subtyping.

Object o = new ArrayList<String>(); List<?> list_string = (List)o; //same as (List<?>)o boolean b = o instanceof List; //same as o instanceof List<?>

Thus, we can infer that beside the cosmetic reason as Mena said and restriction to use genenics, instanceof List and instanceof List<?> are the same.

Linh
  • 325
  • 1
  • 3
  • 10
  • 2
    There is no difference – CoderNeji Aug 05 '15 at 08:58
  • [this](http://stackoverflow.com/a/9165161/2764279) may help you – earthmover Aug 05 '15 at 08:59
  • Duplicate of [Generics and the question mark](http://stackoverflow.com/a/1840022/5020253) – SDekov Aug 05 '15 at 09:00
  • There is a question if almost the same title as your question ... have you at least tried to search for it first? – Tom Aug 05 '15 at 09:03
  • Yes, I saw that question. It's answer explain that both have the same effects since the wildcard is erased at compile time. But why the eclipse recommend `instanceof List>` instead of `instanceof List<>`? – Linh Aug 05 '15 at 09:08
  • Because `instanceof List<>` wouldn't compile and `instanceof List` should be avoided, because raw-types should be avoided (http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it), so it tries to teach you to avoid them. – Tom Aug 05 '15 at 09:15

3 Answers3

3

"List<E> is not a reifiable type" is not the best way to formulate why instanceof won't compile.

The instanceof operator verifies the type of your object at runtime.

As such, type erasure prevents Java from knowing its parametrized type (the type between the <>).

Hence a raw List (equivalent to a List<Object>) or a List<?> (i.e. a List of unknown type) practically mean the same here, although the raw syntax is usually advised against (here, for cosmetic reasons).

tobias_k
  • 74,298
  • 11
  • 102
  • 155
Mena
  • 45,491
  • 11
  • 81
  • 98
0

Perhaps because List is a Raw Type and List<?> is not. Raw types are potentially very dangerous because they can have many strange side effects.

I will post a reference as soon as I can find it.

OldCurmudgeon
  • 60,862
  • 15
  • 108
  • 197
0

List is itself a type. So there wont be any difference if you change the type inside of it.