1

I have recently started reading Core Java. But I am having a hard time grasping the concept of wildcards.

Specifically, I'm confused about the difference between the following:

public class A<T extends {some_class}> {/*...*/}

and

public class A<? extends {some_class}> {/*...*/}

Can anyone help me understand the difference if there is at all?

Sankalp
  • 2,796
  • 3
  • 30
  • 43
  • Duplicate: http://stackoverflow.com/questions/6008241/java-generics-e-and-t-what-is-the-difference – Vaibhav Desai Jul 09 '13 at 04:58
  • Read this http://docs.oracle.com/javase/tutorial/java/generics/bounded.html and http://docs.oracle.com/javase/tutorial/java/generics/upperBounded.html, you won't find better explanations. – Wand Maker Jul 09 '13 at 05:15
  • 1
    The difference is that the second one is not valid syntax. – newacct Jul 09 '13 at 22:39

4 Answers4

1

The difference is that you cannot use the ? elsewhere while you can use T. For example:

public class Foo<T extends Number> {
  T value; // you can declare fields of type T here

  int foo() {
    // Since you said T extends Number, you can call methods of Number on value
    return value.intValue(); 
  }
}

So why would you use ? at all? If you don't need the type. It wouldn't make sense to use it in a class definition any way that I can think of. But you could use it in a method like this:

int getListSize(List<?> list) {
    return list.size();
}

Any kind of method where you're more interested in the overall class and it has a method that doesn't involve the paramaterized type would work here. Class.getName() is another example.

Dave
  • 5,133
  • 21
  • 27
0

They are the same, except with the wildcard you can't refer to the type in the code of your class. Using T names the type.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • If in case of wildcard, one can't refer to the type, then why would one use it? It's confusing me a lot. – Sankalp Jul 09 '13 at 05:04
  • I can't think if a reason. Java has some whacky syntax that while it compiles, it doesn't make sense. Where you use wildcards is in typing variables, where you don't care or need to know the *actual* type, only its abstract (bounded) type. – Bohemian Jul 09 '13 at 05:09
0

In generic code, the question mark (?), called the wildcard, represents an unknown type. The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more specific). The wildcard is never used as a type argument for a generic method invocation, a generic class instance creation, or a supertype.

The Type T is a defined type or known type

Hope this helps

Jono
  • 163
  • 1
  • 5
0
? extends some class   or T extends some_class  

means some_class itself or any of its children and anything that would work with instanceof some_class.

As per conventions T is meant to be a Type and ? is unknown type.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307