Java generics I know (mainly discusses the use of wildcards)

Source: Internet
Author: User
Tags iterable

When using Java generics, you often encounter similar declarations <?extends xxx> or < A super xxx>,xxx represents a class or interface. What the hell does that mean? Read some books today have a little experience, do not know the appropriate or not, forget to discuss it.

The first thing you need to know is that the transition in Java is secure and the transition is not secure. For example, the String class is a subclass of the object class, and we can convert string to object but not to a string, which is obvious because the string class contains methods that the object class does not have;

Let's look at the fact that generics in Java are immutable and immutable, so let's take a look at what is mutable (covariant). An array of String [] strings = new STRING[10];, let the value of strings be paid to an object array: Object [] objects = strings;, this is OK because the covariance of the array, String is a subclass of object, and this is a hidden problem, such as I now write the following code: objects[0] = new Integer (1), which is also allowed, the compiler will compile without warning, However, at run time, the following exception is thrown: Java.lang.ArrayStoreException, the reason is that we store an int type element in an array of type string. But at compile time the compiler is unable to check out, so this is an unsafe hidden trouble.

Having said the covariance of the array, then the invariant of the generics, what is the relationship between an example:list<string> and list<object>? Is it a parent-child relationship? The answer should be no, we cannot write the following code:list<string> strings = new arraylist<> (); list<object> objects = strings; Because there is no parent-child relationship between the two, this property is known as the invariance of generics relative to the covariance of arrays. This nature avoids generics and an unsafe behavior like arrays, which can be checked out at compile time.

And this nature sometimes makes the program somewhat inflexible, there is an example in effect Java, probably a look. Implementation of a stack, the approximate code is as follows:

 Public classStack<e> {      PrivateE [] elements;  Public voidpush (e e) {...}  PublicE pop () {...} //all elements in Src are pushed into the stack       Public voidPushall (iterable<e>src)      {           .....      } //take all elements of the stack out of the plank road in DST       Public voidPopall (iterable<e>DST)    {           .....      }} 

The

Only represents the approximate meaning of the code, which is not reflected in the details. Now we have a stack<number> stack = new stack<> (); Type of stack, I want to put an Integer type of iterable<integer> Integers through the Pushall method into the stack, this is not possible, although the integer is the subclass of number, but iterable<number> and iterable<string> are not related, This time will need to use the < Extends Xxx>. The meaning of this sentence is any XXX sub-class, but specifically what the subclass is not sure, but I know iterable< Extends Number>? Represents the subclass of any number, and knowing this allows the element to be transformed upward to number which is allowed, so we can get the iterable< by some get mechanism. Extends the elements in the number>, but it is not possible to put the elements into them by what set mechanism, because we do not know what the type of this symbol is. Modify the next Pushall method to modify its parameters to Pushall (iterable<? extends e> src) so that we can put the parameters of type iterable<integer> into the Pushall. In effect Java, the SRC is called producer because the data source is provided. Also take a look at another method in the code popall, if I write the following code:

stack<number> stack = new stack<> ();iterable<objcet> dst = ...; Stack.popall (DST); Logically, I put the number class in a container of the object class, which can be said, because object is the parent of number. But the code can not be compiled, because,iterable<objcet> and iterable<number> relationship is not related, then this time need to < Super Number>, this? Represents the parent of any number, what exactly is the parent class? We do not know, but we know that any subclass of number can be put in it, because the subclass of number is always transformed to number, so < Super Number> 's container can be populated by the set method, only the subclass of number is required, but it cannot be returned by a GET method, because I don't know what the parent class is, and I can't convert the parent class into number class by going down. The following modifies the Popall method in the code, changing its parameters to Popall (iterable<? super e> DST) so that I can put the elements in the stack<number> into DST, which is the consumer of DST, Because number is a subclass of object.

The above view is only a little bit of their own ideas, hope to correct, generous enlighten. ^_^

Java generics I know (mainly discusses the use of wildcards)

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.