Get the App
SLTechnology News&Howtos  ›  Development  › 

What does java generics mean?

Shulou Source: shulou.com Published: 2022-05-31 17:50:36 10月23日 Update

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!

Tags: Types parameters transmission learning input content programs that is code instances objects that is ideas situations articles methods more essence concepts Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Tech Info Shulou Technology Linux Microsoft