Home> Java> javaTutorial> body text

what are generics in java

青灯夜游
Release: 2019-05-23 18:15:36
Original
3456 people have browsed it

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!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!