Home >Java >javaTutorial >what are generics in java
Java Generics are a new feature of Java SE 1.5. Generics provide a compile-time type safety detection mechanism, which allows programmers to detect illegal types at compile time. .
The essence of generics is a parameterized type, which means that the data type being operated is specified as a parameter.
Advantages of Generics:
Programs that use generics have many benefits over non-generic code.
1. Code reuse: We can write a method/class/interface and use it for any type we want.
2. Type safety: We can only save one type of object in a generic; it does not allow other objects to be stored.
3. No separate type conversion is required: no type conversion is required for the object.
4. Implement general algorithms: By using generics, we can implement algorithms suitable for different types of objects, and they are also type-safe.
5. Compile time check: Check at compile time, so there will be no problems at run time. (It's always better to know about problems in your code at compile time, rather than having your code fail at runtime).
Recommended related video tutorials: "JAVA Tutorial"
The above is the detailed content of what are generics in java. For more information, please follow other related articles on the PHP Chinese website!