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

What does java generics mean?

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what does java generics refer to". The content of the explanation 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 what java generics refer to.

Concept

1. Generics mean that a type can be passed as a parameter, which is essentially a type parameter. For example, when we define a method, we often specify that a specific class of objects be transferred as parameters.

2. If you use generics, a specific transfer object can be specified as a specific type, without the need to specify a specific type. That is, we transmit a certain type as a parameter.

The difference from Object

If you use Object, you must force the input type to be converted to the desired type, which will result in a package ClassCastException exception if the input type does not match. For example, in the following code, testObj () enters a value of type int, and the program makes an error when executing:

Example

Public void testObj (Object o) {String name= (String) o;} Thank you for reading, the above is the content of "what does java generics refer to". After the study of this article, I believe you have a deeper understanding of what java generics refer to, 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