简体   繁体   中英

Are wildcards in java only applicable to collections?

I am not able to figure out how to declare a parameter in a function or a field in a class using ? wildcard without using any collection. For eg. I can write something like List l1 but how do I use ? without using List or any such collection.

A wildcard generic can be used with any generic type, for example Class<?>

public void myFunction(Class<?> theObject) {
  // your code goes here
}

Every parameterized class can accept wildcards. Just because collections are the most representative example of generics usage, you are not limited to them.

Usually, you use the ? wildcard when you're not sure about the actual parameter class and you want to avoid raw types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM