Java generics Overview

Source: Internet
Author: User

Map m = new HashMap();m.put("key", "blarg");String s = (String) m.get("key");

To allow the program to compile, the result of get () must be forcibly converted to a String type, and the result is expected to be a String. However, it is possible that someone has saved something not a String in the ing. In this case, the above Code will throw ClassCastException.
Ideally, you may come up with the idea that m is a Map that maps the String key to the String value. This allows you to eliminate the forced type conversion in the Code and obtain an additional type check layer, which prevents someone from saving the wrong type keys or values in the set. This is what Generics do.

Benefits of generics:
Introducing generics in Java is a major enhancement in functionality. Not only have the language, type system, and compiler greatly changed to support generics, but also the class libraries have been greatly revised. Therefore, many important classes, such as the collection framework, all of them have become generic. This brings many benefits:
1. type security. The main objective of generics is to improve the type security of Java programs. By knowing the type limits of variables defined using generics, the compiler can validate the type assumptions to a much higher degree. Without generics, these assumptions only exist in the programmer's mind.
2. A popular technique in Java programs is to define such a set, that is, its elements or keys are of the common type, such as "String list" or "String-to-String ing ". By capturing this additional type information in the variable declaration, generics allow the compiler to implement these additional type constraints. Type errors can now be captured during compilation, rather than displayed as ClassCastException at runtime. Moving type checks from runtime to compilation helps you locate errors more easily and improve program reliability.
3. Eliminate forced type conversion. One additional benefit of generics is that many forced type conversions in source code are eliminated. This makes the code more readable and reduces the chance of errors.

Sample Code:

Class Notepad <K, V >{// two generic type private K keys are specified here; // The type of this variable is determined by the external private V value; // The type of this variable is determined by the external public K getKey () {return this. key;} public V getValue () {return this. value;} public void setKey (K key) {this. key = key;} public void setValue (V value) {this. value = value ;}; public class GenericsDemo09 {public static void main (String args []) {Notepad <String, Integer> t = null; // define two generic objects t = new Notepad <String, Integer> (); // the key in the object is String and the value is Integert. setKey ("Tom"); // set the first content t. setValue (20); // set the second content System. out. print ("name;" + t. getKey (); // obtain the information System. out. print (", age;" + t. getValue (); // get information }};

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.