Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse Java generics and equivalent implementation

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to analyze Java generics and equivalent implementation, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

When it comes to generics, there may be many people who don't understand or don't understand. 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, 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 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: java.lang.Integer value= 88-the actual type of T is: java.lang.String value= Hello Gen! Process finished with exit code 0

If you understand this, there will be no problem with basic generic applications and code reading in the future.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report