In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what are the wildcards of Java generics". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What is a generics?
In a word, type parameterization. What do you mean? Parameterization means that we don't know the specific value when we define it, and we don't know the specific value until we actually run it. Type parameterization means that a specific type is not known at the time of definition and is a certain type at actual run time.
How does Java implement generics?
Generics are a feature of many high-level languages. By definition, generics represent the same type at run time, so it's easy to think of List and List using two different Class representations, which is feasible, but Java needs to be compatible with old code and provides container classes before generics are introduced, which (List and List with two different Class) are not compatible with previous old code, so this implementation method is not applicable. So Java bosses came up with another way to implement generics, which is type erasure.
What is type erasure of generics?
Type erasure means that when the bytecode is actually generated, the List defined in the compiler source code becomes List, and the A Class defined in the source code is erased and becomes Object. At the same time, when using it, it will force type conversion and convert the extracted object into an instance of A. This is type erasure.
At first glance, generic erasure doesn't seem to be a big problem, but when you think about it, when you force a type conversion, some information about the type will be lost, which will lead to something that is not as expected. For example, there is a base class A, and its two subclasses B and C, and then we have the following code.
List listA = new ArrayList (); listA.add (new B ()); / / incorrect
The second line of code is not as expected because the listA is expected to put An instead of B. But this doesn't seem to live up to expectations, and we sometimes hope that subclasses can be put into the container. But what happens if this operation is supported? Is it B or C that came out? If it is not clear, then generics are not implemented.
To solve this problem, Java bosses came up with a way to solve these problems.
Generic wildcards?, extends, and super
Before we understand wildcards, what we need to know is, what problems are wildcards invented to solve? At least one problem to be solved is that what is put in the container is what is taken out.
This question, in fact, is divided into two steps, which means to put in the same type of thing. To take it out means to take out something of the same type. In other words, wildcards should be used in different places, where data is written to the container in one place, and data is taken out of the container in another place. If you actually write and read data to the same container in the same code block, you should know the specific type, and you don't need to use wildcards.
? Wildcard character
? Wildcards are called infinite wildcards and indicate uncertainty or indifference to the type.
Extends wildcard
It is generally called the upper-bound wildcard, which means that the range of values is (a subclass of a class, a class). And think about what we said before, the problem that wildcards have to solve? What is put in, what is taken out should be what is taken out. Putting data and fetching data are applied in different scenarios. If we were in the same scene, we wouldn't need to use wildcards, because the type is known.
From the above statement, it is easy to infer.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.