What is the use and point of unbound wildcards generics in Java?

后端 未结 6 1847
渐次进展
渐次进展 2020-12-01 05:12

I don\'t understand what is the use of unbound wildcards generics. Bound wildcards generics with upper boundary makes perfect sense, be

6条回答
  •  萌比男神i
    2020-12-01 06:00

    Using unbounded wildcards only makes sense, AFAIK, when wrapping old code that is not using generics, basically Collections.

    If you look at what you can do with such a generic it's basically nothing. If you have a collection you can't add anything, if you try to read something out you will always get an Objectand so on.

    This in turns helps guaranteeing that you will handle the data in a type safe way, whereas using the raw type would have caused the compiler to ignore any mess you'd make.

    Which methods and fields are accessible/inaccessible through a reference variable of a wildcard parameterized type? from Angelika Langers Java Generics FAQ might be of interest.

提交回复
热议问题