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 understand and equivalently implement Java generics

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

Share

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

How to understand 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.

The advantage 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.

Java 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; / / 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

GenintOb=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

GenstrOb=new Gen ("Hello Gen!")

StrOb.showTyep ()

String s=strOb.getOb ()

System.out.println ("value=" + s)

}

}

Example 2: no Java 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:

Java.lang.Integer

Value= 88

-

The actual type of T is: java.lang.String

Value= Hello Gen!

Will Process finished with exit code 0 be helpful to you after reading 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