In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the relevant knowledge of what is the meaning of Java generics, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this Java generics. Let's take a look at it.
Generics is a very important content in Java, for Java advanced learning is a must to master the knowledge point that this knowledge point is important, if you have read some open source framework code, then you will see that there are many places in the source code to use generics.
Give two random examples, a List and a Map.
Generics are a new feature of Java SE 1.5. generics are parameterized types in nature, that is, the data type being operated on is specified as a parameter. This parameter type can be used in the creation of classes, interfaces, and methods, called generic classes, generic interfaces, and generic methods, respectively. The advantage of introducing generics into the Java language is its security and simplicity.
Before Java SE 1.5, in the absence of generics, the parameter was "arbitrary" through a reference to the type Object. The disadvantage of "arbitrariness" is to explicitly cast a type that requires the developer to predict the actual parameter type. In the case of cast errors, the compiler may not prompt the error and an exception occurs at run time, which is a security hazard.
The advantage of generics is that type safety is checked at compile time, and all casts are automatic and implicit to improve code reuse. ]
Please remember the rough content of the bid above, if you can't remember it, you can memorize it.
Let's take a look at these T, E, K, V,? Who on earth are these sacred?
In fact, these are all wildcards of java generics. At first, if you see so many wildcards, you may think it is enough to drink a pot. In fact, these are no different, just an agreed letter logo, don't be afraid. In fact, you can also use the capital letters AgrameBMagneCMagneD.X MagneZ, which is defined by generics, and it's the same with changing T into A, where T is just the meaning of the name.
E-Element (used in a collection because elements are stored in the collection), E restricts the generic types in each method to ensure that when the same object calls different methods, the type of operation must be the same, and E can be replaced with any other letter. T-Type (Java class), where T represents the specified type at the time of the call, and type inference is performed. K-Key (key) V-Value (value) N-Number (numerical type)? -represents an indeterminate java type, is a type wildcard and represents all types.? No type inference is performed
Give a simple example of chestnut:
Replace T with N, there is no difference in the implementation effect, but we agreed that T represents type, so it is better to follow the agreed specification to increase the readability of the code.
Generic class definition
/ / public class Test {} immediately after the class name
Generic method definition
/ / immediately after the modifier (public) public T Test1 (T t) {}
? Wildcard usage
/ / use List list; List when assigning or declaring variables
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.