Dark Horse programmer-Java generic

Source: Internet
Author: User

-------------------- Android training, Java training, and hope to communicate with you! ----------------------

Java generic is a new feature introduced by Java se1.5. In essence, it is a parameterized type, that is, the Data Type of the lock operation is specified as a parameter. This type of parameter can be used in the creation of classes, interfaces, and methods, which are called generic classes, reverse interfaces, and generic methods respectively.

The advantage of introducing generics in Java is that it is secure and simple. Before Java 1.5, if there is no generics, parameters can be "arbitrary" by referencing objects of the type ", this kind of "arbitrary" brings about the disadvantage of explicit forced type conversion, which requires developers to perform the actual parameter type with it, if the forced type conversion is incorrect, the compiler may not prompt an error and the exception occurs during running, which is a security risk.

The benefit of generics is to check the type security during compilation, and all forced conversions can be automatically and implicitly, improving the code reuse rate.

Now let's take a look at the code to compare the differences between Using Generics and not using generics.

No generic type is used.

ArrayList list = new ArrayList();list.add("welen");String name = (String)list.get(0);System.out.println(name);

Situation after the generic type is used

ArrayList<String> list = new ArrayList<String>();list.add("welen");System.out.println(list.get(0));

If the generic type is not used. add (45); the compiler will not report an error, but an exception will occur during running. If the data added to the set is not of the string type, the compiler will report an error, in this way, the program security can be effectively improved.

Let's look at an example. First, define a generic class.

class B<T>{private T name;public B(T name){this.name = name;}public T getName() {return name;}public void setName(T name) {this.name = name;}}

Execute in the main method

B b = new B("welen");System.out.println(b.getName());B r = new B(45);System.out.println(r.getName());

The compilation and running of the results are successful, and Welen and 45 are printed. A class with the same name can receive different types of parameter instantiation objects. This is another function of generics, improve code reusability.

-------------------- Android training, Java training, and hope to communicate with you! ----------------------
For details, see edu.csdn.net/heima.

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.