Home > Java > javaTutorial > What should you pay attention to when using java generic methods?

What should you pay attention to when using java generic methods?

PHPz
Release: 2023-05-18 08:16:05
forward
951 people have browsed it

1. Notes

Generic methods can exist in either generic classes or ordinary classes. If using generic methods can solve the problem, then you should try to use generic methods.

The type parameter T in the generic method and the type parameter in the generic class are of different types.

2. Example

//注意这个T是一种全新的类型,可以与泛型类中声明的T不是同一种类型。
public <T> void PrinterInfo(T e) {
    System.out.println(e);
}
//调用方法
DataHolder<String> dataHolder=new DataHolder<>();
dataHolder.PrinterInfo(1);
dataHolder.PrinterInfo("AAAAA");
dataHolder.PrinterInfo(8.88f);
Copy after login

What collection classes are there in Java?

Collections in Java are mainly divided into four categories:

1 , List list: ordered, repeatable;

2, Queue queue: ordered, repeatable;

3, Set collection: non-repeatable;

4. Map mapping: unordered, unique keys, but not unique values.

The above is the detailed content of What should you pay attention to when using java generic methods?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template