Home > Java > javaTutorial > How are Java generics instantiated and called?

How are Java generics instantiated and called?

WBOY
Release: 2023-04-23 19:13:05
forward
1570 people have browsed it

1. Generic calling is similar to ordinary method calling, but instead of passing parameters to the method, you pass the type parameters to the Box class itself.

Box<Integer> integerBox;
Copy after login

Type parameter and type variable terminology: Many developers may use the terms "type parameter" and "type variable" confusingly, but these two terms are not the same. When coding, type parameters are provided to create parameterized types. So T in Foo food < t > is a type parameter, and String in Foo f is a type parameter.

2. Generic call to instantiate the class, use the new keyword as usual, but place < integer > between the class name and the brackets.

Usually called a parameterized type (equivalent to the actual parameters of the method).

Box<Integer> integerBox = new Box<Integer>();
Copy after login

The above is the detailed content of How are Java generics instantiated and called?. 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