25th: Generic sets take precedence over arrays

Source: Internet
Author: User

There are two important differences in arrays compared to generic collections.

First, the array is covariant, whereas generics are immutable. The so-called covariant refers to: if the Sub class is a subtype of the Super class, then the array type sub[] is the subtype of super[], which is

That sub[] Array instances can be assigned to the super[] array type variable. Conversely, generics are immutable, so list<sub> and list<super> do not have subtypes and super-type relationships. Other words

You cannot assign a arraylist<sub> instance to a variable of type arraylist<super>.

Second, the array is materialized. As a result, arrays will know and check their element type constraints at run time. If you attempt to save an object of type string in a long array, compile without error.

But running will throw a arraystoreexception exception. Generics, by contrast, are implemented by erasing. So generics simply harden their type information at compile time, and at run-time

Discard their element type information. Erasing is to make generics interoperable with code that does not use generics.

Because of the fundamental differences above, arrays and generic collections do not mix well. such as: New list<e>[], new list<string>[] are not legal.

25th: Generic sets take precedence over arrays

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.