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 implement generics in Java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to achieve generics in Java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve generics in Java".

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 < T > {

Private T ob; / / define generic member variables

Public Gen (T ob) {

This.ob = ob

}

Public T getOb () {

Return ob

}

Public void setOb (T ob) {

This.ob = ob

}

Public void showTyep () {

The actual type of System.out.println ("T is:" + ob.getClass () .getName ())

}

}

Public class GenDemo {

Public static void main (String [] args) {

/ / define an Integer version of the generic class Gen

Gen < Integer > intOb=new Gen < Integer > (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 < String > strOb=new Gen < String > ("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; / / define 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 () {

The actual type of System.out.println ("T is:" + ob.getClass () .getName ())

}

}

Public class GenDemo2 {

Public static void main (String [] args) {

/ / define an Integer version of the 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 the 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.Integervalue= 88Murtel is: java.lang.Stringvalue= Hello genuine 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.

Thank you for your reading, the above is the content of "how to achieve generics in Java". After the study of this article, I believe you have a deeper understanding of how to achieve generics in Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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