I referenced the Java Generics documentation and tried to use the wildcard character "?" in a simple program:
class Unknown <?> {
}
public class UnknownTypes {
public static void main(String s[] ) {
}
}
The wildcard character "?" refers to an Unknown type, therefore, in the Unknown class, I used the type parameter template itself; however, when compiling, I get a compilation error. If I used this, it would work.
class Unknown <T> {
}
If the wildcard character is "?" refers to an unknown type, why can't we use "?" as a type parameter.
Below is the compilation I get.
UnknownTypes.java:1: error: <identifier> expected
class Unknown <?> {
^
UnknownTypes.java:1: error: '{' expected
class Unknown <?> {
^
UnknownTypes.java:10: error: reached end of file while parsing
}
The wildcard character "?" used in conjunction with something else?
source
share