• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is generic ? wildcard replaceable with letters [A-Z]  RSS feed

 
Daniel Gurianov
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Crossposting
Please , excuse me for crossposting , but my question was marked as duplicate on stackoverflow and i think i`ll not be able to get answer on it. Suggested answers does not explain what i`m actually asking, though.
My question on other site
Therefore i try to ask here where atmosphere is much frendly to niewbie questions.

While using generics, are there cases when ? wildcard cannot be replaced with letters [A-Z]? So far , I was able to find only one case, it is when you want to have field pointing on generic instance without making class generic.



In case above, as I understand, you cannot use [A-Z] without generalize OneClass. Are there any other cases, when there is no way except to use ? wildcard instead of letter [A-Z]?
Thank you.

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In what you posted, the ? must be replaced with a type, not just any letters. Is that your question?
 
Daniel Gurianov
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:In what you posted, the ? must be replaced with a type, not just any letters. Is that your question?


No.

In all cases i saw so far, ? wildcard can be replaced with any letter placehoder. Compiler will resolve this placeholder from either method or class generalisation. In case what i have posted as example, i cannot use any letter placeholder (i.e [A-Z]) , because compiler cannot figure out from where take a type when resolving this placeholder. So, the only option for me (if i`m strictly against generalise OneClass) is to use ? wildcard that will say to compiler "LinkedList with any type objects inside".
My question is : are there any other cases, where use of ? wildcard is inevitable and you cannot replace it with letter placeholder?
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute is correct. The way you have coded it, you need to use a class name that the compiler can resolve. If you want to do what I think you are trying to do, you need something like this instead:

BTW, it's better to follow convention for these "placeholders" which are otherwise properly referred to as "type parameters" - T for Type, E for Element, etc. You can find more information about generic type parameters in the Java Tutorials
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For instance, this works fine:

 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a fairly subtle difference between List<T> and List<?>.

List<T> defines a list of a specific type T, where T is going to be specified somewhere. So it could be in a generic class, in which case T is given when creating the object:

So in this case the TestClass<String> contains a List<String>. Or it could be in a generic method, in which case T is specified when the method is called.

Whereas List<?> defines a reference that can point at any type of list at all. There's nothing that fixes the compiler to just allow a specific type.

There are cases where you can potentially use either approach, but one result of this difference is that you can't actually create a List<?>. It only acts as a reference type. Whereas you can create a List<T> once T is known.
 
Daniel Gurianov
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the answers.

From all that i can see now, i`m starting to believe that ? can be completely replaced with letters , except some really exotic cases, which Java coding conventions does the best to make you avoid.
At leat all examples i faced in books till now , i was able to rewrite with letter placeholders instead of ?.
And that is what i wanted to understand.
I thought that ? is some feature , invented to cover some specific cases , where there are no other soutions possible. Now i see that it is rather shortcut to make code shorter, cleaner and neat.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's perhaps a little bit of a hasty generalization. Consider the List<E> interface from java.util, you know, the List class which is very widely used. It has a method whose signature is

public boolean addAll(Collection<? extends E> c);


This isn't just a shortcut for a more complicated version which doesn't use wildcard types. There isn't any such more complicated version.

You probably didn't notice, but every time you replaced a wildcard type by a named type, you made that named type a type parameter of the containing class. So in Knute's example you have



rather than



So to replace that wildcard type that I mentioned in the List.addAll() method by a named type, you'd have to include the named type in the interface definition. So then you'd have to have



But that doesn't make sense because the X isn't actually anything to do with the List object, it refers to some other Collection object which you might or might not be using in your code. Making people who use a List specify a type which isn't relevant to their code is just a nonsensical design. Not to mention that you might be calling the addAll() methods with different values of X in the same code base, or with values of X which can't be determined at compile time -- so no, it isn't just a shortcut, it's a necessary feature of the language.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!