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

T and? in java generics. What's the difference?

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

Share

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

Today I will talk to you about T and T in java generics. What is the difference, many people may not understand, in order to let you better understand, the editor summed up the following content, I hope you can gain something according to this article.

T stands for a type.

Add to the class:

Class SuperClass {}

To add to the method:

Public void fromArrayToCollection (T [] a, Collection c) {}

(free video tutorials to share: java video tutorials)

Generic parameters are used in the parentheses on the method. If generics are passed in the class, the generic parameters above the type can be called, provided that the generics used in the method are consistent with the generics passed from the class.

Class People {public void show (T a) {}}

T extends T2 refers to T2 or a subtype of T2.

? Is a wildcard, which generally refers to all types.

It is generally used to define a reference variable, and the advantage of this is that, as shown below, defining a reference variable of sup can point to multiple objects.

SuperClass sup = new SuperClass ("lisi"); sup = new SuperClass (new People ()); sup = new SuperClass (new Animal ())

If you do not use a fixed type, then:

SuperClass sup1 = new SuperClass ("lisi"); SuperClass sup2 = new SuperClass ("lisi"); SuperClass sup3 = new SuperClass ("lisi")

That's it? The benefits of wildcards.

After reading the above, what do you think of T and? in java generics? Do you have any further understanding of what is the difference? If you want to know more knowledge or related content, 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