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 will explain in detail the understanding of Java generics and what the equivalent implementation is. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Generics are a new feature of Java 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 the introduction of Java generics is that they are safe and simple.
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, increasing code reuse.
Generics also have some rules and restrictions in use:
1. The type parameters of generics can only be class types (including custom classes), not simple types.
2. The same generic type can correspond to multiple versions (because the parameter type is uncertain), and different versions of generic class instances are incompatible.
3. Generics can have multiple type parameters.
4. The parameter types of generics can use extenders statements, for example. It is customary to become a "bounded type".
5. The parameter types of generics can also be wildcard types. For example, Class classType = Class.forName (java.lang.String)
Generics also have interfaces, methods, and so on, and they have a lot of content, and it takes a lot of effort to understand and skillfully apply them. Here are two examples I wrote about generics (based on my impression) to achieve the same function, one using generics and the other not using generics. I can quickly learn the application of generics by comparison. I basically learned 70% of the content of generics.
Example 1: using Java generics
Public class Gen {private T ob; / / defines the generic member variable public Gen (T ob) {this.ob = ob;} public T getOb () {return ob;} public void setOb (T ob) {this.ob = ob;} public void showTyep () {System.out.println ("the actual type of T is:" + ob.getClass (). GetName ()) }} public class GenDemo {public static void main (String [] args) {/ / defines an Integer version of the generic class Gen Gen intOb=new Gen (88); intOb.showTyep (); int I = intOb.getOb (); System.out.println ("value=" + I); System.out.println ("-") / / define a String version of the generic class Gen, Gen strOb=new Gen ("Hello Gen!"); strOb.showTyep (); String s=strOb.getOb (); System.out.println ("value=" + s);}}
Example 2: no generics are used
Public class Gen2 {private Object ob; / / defines a generic type member public Gen2 (Object ob) {this.ob = ob;} public Object getOb () {return ob;} public void setOb (Object ob) {this.ob = ob;} public void showTyep () {System.out.println ("T's actual type is:" + ob.getClass (). GetName ()) }} public class GenDemo2 {public static void main (String [] args) {/ / define an Integer version of class Gen2 Gen2 intOb = new Gen2 (new Integer (88)); intOb.showTyep (); int I = (Integer) intOb.getOb (); System.out.println ("value=" + I); System.out.println ("-") / / define a String version of class Gen2 Gen2 strOb = new Gen2 ("Hello Gen!"); strOb.showTyep (); String s = (String) strOb.getOb (); System.out.println ("value=" + s);}}
Running result:
The result of running Demo in the two examples is the same, and the output from the console is as follows:
The actual type of T is:
The actual type of java.lang.Integer value= 88-T is: java.lang.String value= Hello Gen! Process finished with exit code 0 on the understanding of Java generics and how the equivalent implementation is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.