Home > Java > javaTutorial > What does java generics refer to?

What does java generics refer to?

王林
Release: 2023-04-15 09:22:07
forward
1609 people have browsed it

Concept

1. Generics means that types can be passed as parameters, which are essentially type parameters. For example, when we define a method, we often specify that a specific class of objects be passed as parameters.

2. If generics are used, a specific transfer object can be specified as a specific type without specifying a specific type. That is, we pass a certain type as a parameter.

Difference from Object

If you use Object, you must force the input type to be converted to the required type. If the input type does not match, it will cause the package ClassCastException. abnormal. For example, in the following code, testObj() inputs a value of type int, and an error occurs when the program is executed:

Example

public void testObj(Object o){     String name= (String) o; }
Copy after login

Thank you for reading, the above is" What does java generics refer to?" After studying this article, I believe that everyone has a deeper understanding of what java generics refers to. The specific usage needs to be verified in practice. This is our website. The editor will push more articles on relevant knowledge points for you. Welcome to pay attention!

The above is the detailed content of What does java generics refer to?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template