Java Generic Learning notes-(i) Introduction to Generics

Source: Internet
Author: User

First, what is generic:
The purpose of generics is to specify the type of data that a class, interface, or method can accept. Just as you specify parameters when declaring a method, we can also specify its "type parameter", which is generic, when declaring a class, interface, or method. The difference is that when declaring a method, we specify a value for its argument and a data type for its generic type.
Second, the basic use way:
The above concept wordy a lot of, in fact, I wrote all tired. The simplest and most effective way to learn is to use one:

1 New Arraylist<string> ();

This specifies that only string types of data can be stored in the list. But there are a few things to note:

    1. The <> must be a reference data type, not a basic data type such as int, double;
    2. Before and after the generics must be consistent. As shown above, a LIST<STRING> is declared at the time of Declaration, and at the time of creation a new arraylist<string> () is required, not a arraylist<integer> () or something, Since this rule was introduced, a new feature called The Diamond Generic (The Diamond) was released at JDK1.7, meaning that the following generics could be omitted directly written as <>
      1 New Arraylist<> ()
    3. A generic can be any reference type, so it can also be defined as an object type. But object is the parent of all data types, plus no extra, no eggs.

III. Benefits of using generics:

    1. Improved security: Convert run-time errors to compile time. If we assign a value to an object that does not conform to its generic rules, it compiles an error.
    2. Eliminate the hassle of strong turns: for example, if we're using list, if we don't use generics, such as:
      1 New ArrayList (); 2 l.add ("abc"); 3 string s = (string) l.get (0);

When an element is taken from a list, its type is the default object, and we must transform it down to a string to use. Instead, use generics:

1 New Arraylist<>(); 2 l.add ("abc"); 3 String s = l.get (0);

You can guarantee that both the deposit and the fetch are of type string and do not have to be cast.

Java Generic Learning notes-(i) Introduction to generics

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.