Java generic and java generic

Source: Internet
Author: User

Java generic and java generic
1. Understanding of generics

Generic is a new feature of JDK1.5. In essence, generic is a parameterized type, that is, the operated data type is specified as a parameter. This type of parameter can be used in the creation of classes, interfaces, and methods. They are called generic classes, generic interfaces, and generic methods. Their functions are similar to templates in C ++ ), but not exactly. Generics are mainly used in set interfaces in java.

  • Added a set that supports generics. You can remember the element types in the set and check the element types during compilation.
  • Added a set of generic support to make code more concise and programs more robust.
  • Java generics also provide enumeration, reflection, and other functions to make the program more flexible.
Ii. Generic usage
  • If no generic type is used, exceptions of the type are not checked during compilation.
  • The generic type is used, and the type is automatically checked during compilation.
1 public class GenericsDemo {2 3 public static void main (String [] args) {4 // noGenerics (); 5 generics (); 6} 7 8 // when generics are used 9 private static void generics () {10 ArrayList <String> strList = new ArrayList <String> (); 11 strList. add ("liming"); 12 strList. add ("wangfeng"); 13 // try to add an Integer object here. The compilation error 14 // strList. add (100); 15 for (int I = 0; I <strList. size (); I ++) {16 // you do not need to forcibly convert 17 String = strList. get (I); 18 System. out. println (string); 19} 20} 21 22 // when the generic type is not used 23 private static void noGenerics () {24 ArrayList strList = new ArrayList (); 25 strList. add ("liming"); 26 strList. add ("wangfeng"); 27 // an Integer object 28 strList is accidentally added here. add (100); 29 for (int I = 0; I <strList. size (); I ++) {30 // all elements in the strList are of the Object type, so forced conversion is required, however, the last element will encounter ClassCastException 31 // we try to convert the Integer type of the last element to String type 32 String string = (String) strList. get (I); 33 System. out. println (string); 34} 35} 36}
Iii. Extensive understanding

Applicability of generics: When the referenced data types of operations in interfaces, classes, and methods are uncertain, objects are extended and can be expressed in generics. This avoids the trouble of strong conversion and transfers the running problem to the compilation period.

However, pay attention to the following points:

  • Generic Type parameters can only be class types (including custom classes), not simple types, and generic type parameters can have multiple
  • The type of the generic type depends on the type passed by the caller. If it is not passed, the default type is Object.
  • When an object is created using a class with generics, the specified generics on both sides of the equation must be consistent.
  • Wildcard can be used on either side of the equation, but not on the other side (backward compatibility)

 

Write it here first, and then introduce it in detail...

 

Related Article

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.