Java Learning notes-generics

Source: Internet
Author: User

If T both inherits the class and specifies the interface, use the following method:

public class Some<t extends Iterable<t> & comparable<t>>{}

Java generics do not have a covariance (if B is a subclass of a, and node<b> can be considered a node<a>, then node has a covariance), but you can use a type wildcard character? and extends to declare the variable so that it achieves a similar covariance.

node<apple> Apple = new Node<> (new Apple (), null); node<? Extends fruit> Fruit = Apple;  Similar to the effect of co-degeneration; if node<fruit> Fruit = Apple; The compilation error will be

If the statement does not matchextends, the default is? extends Object, such as:

Node<?> fruit = null;  Equivalent to <span style= "font-family:arial, Helvetica, Sans-serif;" >node<? Extends object></span>

Note: this differs from the declaration node<object>, if it is declared as Node<object>, then only reference to node<object> Instance , the following program compiles an error:

node<object> node = new node<integer> (1, null);  No co-degeneration

Should be:

node<?> node = new node<integer> (1, NULL);


Once used? With the extends limit the type of T, you can only get the object by the name of the T declaration or specify the name of the T declaration as null, in addition to other specified actions

node<? Extends fruit> Fruit = new node<> (new Apple (), null), Object o = Node.value;node.value = null; Apple Apple = node.value;  Compilation Error Node.value = Apple Apple ();   Compile error

The above program only knows if the object type of value reference will be inherited fruit, but will it actually be apple or banana? If Node.value is actually an instance of banana, it is wrong to assign Apple type to Apple


Other:

Java's generic syntax is only checked at compile time, unknown at execution time, and actually only known as the object type (also known as "type Erasure"), such as:

List<integer> List1 = new arraylist<> (); list<string> list2 = new arraylist<> (); System.out.println (List1.equals (List2));  Output true














Java Learning notes-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.