Generic description of Java generics and collections

Source: Internet
Author: User

You can use angle brackets with one or more arguments when declaring an interface and class, but when you declare a variable that belongs to an interface or class, or when you create a class instance, you need to provide their specific type. Let's take a look at the following example

List<string>words = new arraylist<string> (); Words.add ("Hello"); Words.add ("world!"); String s = words.get (0) +words.get (1); Assert S.equals ("Hello world!");

  

,arraylist<e> implements the interface List<e> in the collection frame, the simple code above declares a variable words that contains a list of strings, and creates a ArrayList instance that adds two strings to the list. and take them out. In the previous generics of Java, the same code would write:

List words = new ArrayList (); Words.add ("Hello"); Words.add ("world!"); String s = ((string) words.get (0)) + ((String) words.get (1)) Assert S.equals ("Hello world!");

  

if there is no generic type, the parameter type is ignored. But when the element is removed from the list, you must explicitly use the coercion type conversion. in fact, it is exactly the same as the bytecode file compiled from the two source files above. We say this is a generic erase. These types are the same type List at run time because of List<integer>,list<string>, and list<list<string>>, and so on. We will refer to the process from the first program to the second program as a generic wipe. The term erase is a bit inaccurate, because the program erases the parameter type but increases the cast.

Generics are not used when the generic implicitly uses the same cast to display the representation. If this conversion fails, then it is difficult to debug with the generic write code. Because the following preconditions are said to explain why generics are reliable:
Implicit conversions that are added through generic compilation will never fail.

The important thing about this condition is that this is only valid for the compiler when it has no published, unchecked warning. We'll talk to some extent about what's causing the unchecked warnings that have been released and how to minimize their impact. Generics implemented through generic erasure have many benefits. It makes things simple, and in such generics it does not add anything fundamentally new; it makes things small, and accurately it only has a list implementation, not a version for each type; it erases the process because in the same library we can access the call in the form of generics or non-generics. The last point is well worth elaborating, you will not encounter the problem that makes you unhappy when you use two different versions of the class library: Use 1.4 or a previous non-generic version, and a generic version that uses versions 5 and 6. At the bytecode level, code that does not apply generics is the same as code that uses generics. There is no need to immediately turn to full-pan molding---You can upgrade your code by updating only one package at a time, one way to start using generics. We can even parse out the generic type you declared for legacy large code.


Another effect of generic erasure is that the type of the array and the parameterized type are different in use. Run New String[size] assigns an array and additionally stores the String type component in this array. In contrast, run New arraylist<string> () assigns a list, but does not otherwise store the list of element types in this list. In terms of terminology, we say that Java materializes the type of an array element. However, there is no element type for the materialized list.

It's hard.

Generic description of Java generics and collections

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.