1. Explanation
There are two reasons why new T() cannot be used in generic code. One is that the type cannot be determined due to erasure; but it cannot be determined whether T contains No-argument constructor.
We use the factory pattern generic method to create instance objects, create IntegerFactory factories, and create Integer instances. If the code changes later, a new factory type can be added.
2. Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
The above is the detailed content of How to define data types in Java. For more information, please follow other related articles on the PHP Chinese website!