Java Generics Benefits and cases

Source: Internet
Author: User

Java Generics are a new feature of Java SE 1.5, where the essence of generics is a parameterized type, meaning that the data type being manipulated is specified as a parameter. This type of parameter can be used in the creation of classes, interfaces, and methods, called generic classes, generic interfaces, and generic methods , respectively.

& nbsp;      Generics (Generic type or generics) is an extension of the Java language type system to support the creation of classes that can be parameterized by type. You can think of a type parameter as a placeholder for the type that you specify when you use a parameterized type, as if the form parameter of the method is a placeholder for the value passed at run time.
        can see the motivations for generics in the collection Framework (Collection framework). For example, the map class allows you to add an object of any class to a map, even if the most common scenario is to save an object of a particular type (such as a String) in a given map (map).
because Map.get () is defined to return Object, it is generally necessary to cast the result of Map.get () to the desired type, as shown in the following code:

Map m = new HashMap ();
M.put ("Key", "Blarg");
String s = (string) m.get ("key");

to get the program to compile, you must cast the result of the Get () to string, and you want the result to be really a string. But it is possible that someone has saved something that is not a String in the map, so the above code will throw classcastexception.
Ideally, you might come to the view that M is a map that maps a string key to a string value. this allows you to eliminate coercion of type conversions in your code and to obtain an additional type-checking layer that prevents someone from saving the key or value of the wrong type in the collection. This is the work that generics do.

Benefits of Generics


The introduction of generics in the Java language is a large feature enhancement. Not only has the language, type system, and compiler changed significantly to support generics, but class libraries have also been overhauled, so many important classes, such as the collection framework, have become generics.

This brings a number of benefits:

1, type safety. the primary goal of generics is to improve the type-safety of Java programs. By knowing the type limits of variables defined using generics, the compiler can validate the type hypothesis at a much higher level. Without generics, these assumptions exist only in the programmer's mind (or, if you're lucky, in code comments).

2. Elimination of forced type conversions. a side benefit of generics is the elimination of many coercion type conversions in the source code. This makes the code more readable and reduces the chance of error.

3, the potential performance benefits. generics are possible for larger optimizations. In the initial implementation of generics, the compiler enforces type conversions (without generics, programmers specify these coercion type conversions) into the generated bytecode. But the fact that more type information is available for compilers is possible for future versions of JVM optimizations. Because of the way generics are implemented, generics are supported (almost) without the need for JVM or class file changes. All work is done in the compiler, and the compiler generates code that is similar to what is written when there is no generics (and coercion of type conversions), just to be more secure type.


The benefits of introducing generics into the Java language are simple and secure. The benefit of generics is that it checks for type safety at compile time, and all casts are automatic and implicit, increasing the rate of reuse of the code.

Generics also have some rules and restrictions in use:
1. A generic type parameter can only be a class type (including a custom class) and cannot be a simple type.
2, the same generic can correspond to multiple versions (because the parameter type is indeterminate), different versions of the generic class instances are incompatible.
3, generic type parameters can have more than one.
4. Parameter types of generics can use extends statements, such as <t extends Superclass>. Become a "bounded type" in practice.
5. The parameter type of a generic type can also be a wildcard type. For example class<?> ClassType = Class.forName (Java.lang.String);
Generics and interfaces, methods and so on, a lot of content, it takes some effort to understand mastery and proficiency in the application. Here are the two examples I used to know about generics (based on impressions), to achieve the same functionality, one that uses generics, one that is not used, and that, by contrast, can quickly learn generics, learning that this basically learns the content of generics 70%.


Reference case: Java generics summary case

This article from the "extraordinary life-seekers of Knowledge" blog, reproduced please contact the author!

Java Generics Benefits and cases

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.