What's the purpose behind wildcards and how are they different from generics?

后端 未结 4 579
误落风尘
误落风尘 2020-12-01 06:40

I\'d never heard about wildcars until a few days ago and after reading my teacher\'s Java book, I\'m still not sure about what\'s it for and why would I need to use it.

4条回答
  •  悲哀的现实
    2020-12-01 07:17

    The difference between your 2 examples is simply that the first one is a list of generic/general animals - you can therefore add any type of animal to it, and any instance of a subclass of type Animal. (e.g. It can contain some dogs, some cats, some porcupines...) Whereas the second - List - will be a list of one specific subtype of class animal. It can be any one you choose (this is set each time at runtime), but only one. It will either be a list of dogs, or a list of cats, or a list of turtles... etc.

提交回复
热议问题