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

How are Java generics instantiated and called?

WBOY
WBOYforward
2023-04-23 19:13:051541browse

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;

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>();

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete