3

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.

Rollerball
  • 12,618
  • 23
  • 92
  • 161

2 Answers2

3

Suppose we have a generic declaration

interface Foo<T>
    T get();
    void set(T);
    void bet();

A raw type Foo is equivalent to a type declared as

interface Foo
    Object get();
    void set(Object);
    void bet();
    // all generics info are stripped

For example, in Java 5 we have List<E>, its raw version List contains the exact same method signatures as the pre-java5 List interface. Raw type is used for backward compatibility.

Raw List is pretty close to List<Object>; but very different from List<?>


An object foo of type Foo<?> has the type of

interface Foo<X>
    X get();
    void set(X);
    void bet();

for some definitive, albeit unknown, type X. Though X is unknown, we can still invoke foo.get() and foo.bet(). But we can't invoke foo.set(a) because there's no way to know whether a is of the unknown type X - unless a is null.

ZhongYu
  • 19,446
  • 5
  • 33
  • 61
-1

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).

Community
  • 1
  • 1
Sebastian
  • 5,177
  • 4
  • 30
  • 47
  • 1
    No, they are not identical(at least at compile time). They are quite the opposite, you can add anything into a Raw type but cannot add anyting(except null) into unbounded wilcards list.But i am not sure if they are identical or not during run time tough – PermGenError Mar 22 '13 at 11:14
  • They are identical during runtime, see first link. Why did you delete your first answer if you still defend it now? – Sebastian Mar 22 '13 at 11:17
  • my first part was right. as it was regarding compiler time, but my last part during run time was total bogus. :P, that was the reason i deleted my answer. however, you should mention that in your answer regarding run time . :) – PermGenError Mar 22 '13 at 11:18
  • 1
    At runtime all `List` and `List` are identical, but that has nothing to do with the unbounded wildcard specifically, thus isn't an explanation. – Marko Topolnik Mar 22 '13 at 11:22
  • hmm just checked the byte code generated for both `List>` and List. they are absolutely identical. but not sure we can consider them identical thou. but as far as i underdtand from marko's comments in my deleted answer.We shouldn't really talk about generics at run time . :) – PermGenError Mar 22 '13 at 11:23
  • @MarkoTopolnik thanks marko. it does make sense now. I just checked the byte code for raw type, parameterized type and unbounded type list. all produced same byte code. :) – PermGenError Mar 22 '13 at 11:25
  • 1
    @PermGenError If you have an upper-bounded parameter, that will produce downcasts in the bytecode (for example, on the result of `get`). Whether we count that as "different types at runtime" is debatable, but the fact stays that the runtime type of the instance in question is still exactly the same. – Marko Topolnik Mar 22 '13 at 11:53