Java Learning Note 29 (Set Frame III: generics)

Source: Internet
Author: User

The concept of generics:

Simply put, it is the same method (class) that can accept different data types and run the corresponding results without security issues

The previous article has a code like this:

No collection type defined, iterator type

 Packagedemo;Importjava.util.ArrayList;Importjava.util.Collection;ImportJava.util.Iterator; Public classCollectiondemo { Public Static voidMain (string[] args) {//collections can store objects of any type//collection, you do not specify a stored data type, or you can storeCollection C1 =NewArrayList (); C1.add ("ABC"); C1.add ("Def"); Iterator it1=C1.iterator ();  while(It1.hasnext ()) {//It.next Gets the object type, castString S1 =(String) it1.next ();        System.out.println (S1.length ()); }    }}

There is a hidden danger: If Add (1), automatically boxed into an integer type, cannot be converted to a string, the type conversion exception occurs

The concept of generics is presented in Java to solve this problem:

 Packagedemo;Importjava.util.ArrayList;Importjava.util.Collection;ImportJava.util.Iterator; Public classGenericdemo { Public Static voidMain (string[] args) {function (); }         Public Static voidfunction () {Collection<String> C1 =NewArraylist<string>(); C1.add ("ABC"); C1.add ("Def"); Iterator<String> it1 =C1.iterator ();  while(It1.hasnext ()) {String S1=It1.next ();        System.out.println (S1); }    }}

At this point, if Add (1), will not be passed at compile time, solve the security problem

In fact, the generics here, is pseudo-generic, here is just a compilation means, if not a string type, compilation cannot pass, otherwise the compilation succeeds

There is no generics in the compiled class file, but it is safe to run the final operation because of the type problem that is handled during the compilation process.

Generic method (understand, no actual application value):

 Packagedemo;Importjava.util.ArrayList; Public classGenericdemo { Public Static voidMain (string[] args) {ArrayList<Integer> array =NewArraylist<integer>(); //method ToArray generic method in ArrayList setArray.add (123); Array.add (456); Integer[] I=Newinteger[(Array.size ())]; Integer[] J=Array.toarray (i);  for(Integer k:j) {System.out.println (k); }    }}

There are also generic interfaces, generic classes, and so on, designed to address security issues and facilitate users, and to bring enhanced for loops

A generic wildcard character? :

 Packagedemo;Importjava.util.ArrayList;Importjava.util.Collection;ImportJava.util.HashSet;ImportJava.util.Iterator; Public classGenericdemo { Public Static voidMain (string[] args) {ArrayList<String> array =NewArraylist<string>(); Array.add ("ABC"); Array.add ("Def"); HashSet<Integer> set =NewHashset<integer>(); Set.add (123); Set.add (456);        Iterator (array);    Iterator (set); }    //requires that you define a method that iterates over two sets at a time     Public Static voidIterator (collection<?>c) {Iterator<?> it1 =C.iterator ();  while(It1.hasnext ()) {System.out.println (It1.next ()); }    }}

Java Learning Note 29 (Set Frame III: generics)

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.