Hi directly from a java tutorial provided by Oracle http://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
static void filter(Collection<?> c) {
for (Iterator<?> it = c.iterator(); it.hasNext(); )
if (!cond(it.next()))
it.remove();
}
I am aware of the type erasure at compilation time. And I am aware also of that a type (unbounded) is going to be substituted with Object. Being aware of that what is going to do the compiler with the unbounded wild card at compilation time? Just removing it as it was a raw type?
Thanks in advance.
Suppose we have a generic declaration
A raw type
Foo
is equivalent to a type declared asFor example, in Java 5 we have
List<E>
, its raw versionList
contains the exact same method signatures as the pre-java5List
interface. Raw type is used for backward compatibility.Raw
List
is pretty close toList<Object>
; but very different fromList<?>
An object
foo
of typeFoo<?>
has the type offor some definitive, albeit unknown, type X. Though X is unknown, we can still invoke
foo.get()
andfoo.bet()
. But we can't invokefoo.set(a)
because there's no way to know whethera
is of the unknown typeX
- unlessa
isnull
.This has been answered before: Java Generics - What's really in a unbounded wildcard? and Difference between an unbound wildcard and a raw type for example.
So, yes,
<?>
and raw types are identical (at run time).