Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Java in the inverter and covariance     - Linux System Getting Started Tutorial: How to Force Change your password at next logon Linux (Linux)

- Linux System Getting Started Learning: The Linux anacron command (Linux)

- Ubuntu 14.10 Apache installation and configuration (Server)

- Linux system last command usage (Linux)

- Oracle can not change the tablespace to backup mode in non-archive mode (Database)

- Ubuntu users how to install the latest Nvidia graphics drivers (Linux)

- Beautiful start Ubuntu installation tool Duck Launcher 0.64.5 (Linux)

- Shell generated using automated configuration script Orabbix (Database)

- Parameters of the extra port MySQL 5.6 (Database)

- A process of how to get the current traffic in GNU Linux (Linux)

- Ubuntu users to install Xtreme Download Manager (Linux)

- A simple shell script for monitoring in Linux (Linux)

- Install NetBeans IDE 8.0 on Ubuntu, Linux Mint, Elementary OS, and Debian (Linux)

- The YUM package management under Linux (Linux)

- Oracle database with test data insertion speed (Database)

- Ubuntu 14.04 LTS to compile the source code Android4.4.2 (Linux)

- CentOS / Linux NIC set IP address configuration (Linux)

- Installation and Configuration Munin monitoring server on Linux (Server)

- Configuring Android NDK development environment for Mac OS X (Linux)

- RHEL5 multipath configuration (Linux)

  Java in the inverter and covariance
  Add Date : 2018-11-21      
  Look at the following piece of code

Number num = new Integer (1);
ArrayList list = new ArrayList (); // type mismatch

List list = new ArrayList ();
list.add (new Integer (1)); // error
list.add (new Float (1.2f)); // error
Some may wonder why the Number object can be instantiated by the Integer, and ArrayList object can not be instantiated by the ArrayList ? list of element is its declaration Number Number or derived class, why not add Integer and Float? To solve these problems, we need to understand Java in the inverter and covariant generic and wildcard usage.

1. The inverter and covariance

Before the introduction of the inverter and covariant, first introduced Liskov Substitution Principle (Liskov Substitution Principle, LSP).

Liskov Substitution Principle

LSP in 1987, proposed by Barbara Liskov, which is defined as follows:

All references to base class (parent class) must be able to place the transparent use of its subclasses of objects.

LSP contains the following four meanings:

Wholly owned sub-class of the parent class method, and concrete subclasses must implement the abstract methods of the parent class.
Subclass can add their own methods.
When a subclass overrides the parent class or implement the method, more liberal than the parent class method parameter method.
When a subclass overrides the parent class or implement a method that returns a value more stringent than the parent class method.
In front of the two meanings it is better understood, the latter two meanings will be explained in detail below. According to LSP, we instantiate the object, which can be used to instantiate subclasses, such as:

Number num = new Integer (1);

Covariant and inverter used to describe the inheritance type conversion (type transformation) after its definition: If A, B indicates the type, f (⋅) indicates the type of conversion, <= represent inheritance relationships (for example, A <= B means that A is derived from the B sub-class);

f (⋅) is an inverter (contravariant), there are f (B) <=f (A) was established when A <= B;
f (⋅) is the covariance (covariant), there are f (A) <=f (B) was established when A <= B;
f (⋅) is unchanged (invariant) when A <= B above two equations are unsubstantiated, that there is no inheritance relationship between them f (A) and f (B).
Type Conversion

Next, we look at the common types of Java conversion covariance, inverter or invariance.


So f (A) = ArrayList < A >, then when f (⋅) inverter, covariant or invariant it? If the inverter, the ArrayList < Integer > is ArrayList < Number > parent type; if it is covariant, the ArrayList < Integer > is ArrayList < Number > subtype; if it is the same, no two mutual inheritance. Opening code used ArrayList < Integer > list of instantiated object error, then generics are the same.


So f (A) = [] A, the array is easy to prove covariant:

Number [] numbers = new Integer [3];

Iamzhoug37 through discussions with friends, updating as follows.

Call the method result = method (n); according to the substitution principle Liskov, passed as parameter n should be a subtype method parameters, namely typeof (n) <=typeof (method's parameter); result should be the method returns the base value types, typeof (methods's return) <=typeof (result):

static Number method (Number num) {
    return 1;

Object result = method (new Integer (2)); // correct
Number result = method (new Object ()); // error
Integer result = method (new Integer (2)); // error
In Java 1.4, the subclass cover (override) the parent class method, the type of form to participate in the return value must be consistent with the parent class:

class Super {
    Number method (Number n) {...}

class Sub extends Super {
    Number method (Number n) {...}
Allow Java 1.5 from the beginning, the child class override the parent class method covariant return more specific types:

class Super {
    Number method (Number n) {...}

class Sub extends Super {
    Integer method (Number n) {...}
2. The generic wildcard

Implement generic covariant and inverter

Java generics are the same, but sometimes the need to achieve an inverter with covariant, how to do it? In this case, the wildcard comes in handy?:

< ? Extends > implements generics covariant, such as:
List < ? Extends Number > list = new ArrayList < Integer > ();
< ? Super > implements a generic inverter, such as:
List < ? Super Number > list = new ArrayList < Object > ();
extends and super

Why (opening tag) List < ? Extends Number > list compilation errors occur add Integer and Float? First, we add a look of realization:

public interface List < E > extends Collection < E > {
    booleanadd (E e);
When you call the add method, automatically becomes a generic E < ? Extends Number >, which represents some type of a list held by the Number type and the Number subclass, which includes but is not specific to the type Integer an Integer type (Integer like a spare tire, like !!!), so the compiler error occurred while add Integer.

To be able to call the add method can be used to achieve the super keyword:

List < ? Super Number > list = new ArrayList < Object > ();
list.add (new Integer (1));
list.add (new Float (1.2f));
indicates the type of a list held by the type of Number and Number of the base class, where the Integer and Float must be available for this type of a subclass; so add methods can be called correctly. As can be seen from the above example, it extends determine the upper bound of the generic, generic and super determined lower bound.


Now comes the question: what time extends when used with super it? "Effective Java" gives the answer:

PECS: producer-extends, consumer-super.

For example, a simple Stack API:

public class Stack < E > {
    public Stack ();
    publicvoidpush (E e):
    public E pop ();
    publicbooleanisEmpty ();
To achieve pushAll (Iterable < E > src) method, the elements of each stack src:

publicvoidpushAll (Iterable < E > src) {
    for (E e: src)
        push (e)
Suppose you have an instance of Stack < Number > object stack, src have Iterable < Integer > and Iterable < Float >; type mismatch error occurs when you call pushAll method because Java generics are immutable, Iterable < Integer > and Iterable < Float > is not Iterable < Number > subtype. Therefore, should be changed

// Wildcard type for parameter that serves as an E producer
publicvoidpushAll (Iterable < ? extends E > src) {
    for (E e: src)
        push (e);
To achieve popAll (Collection < E > dst) method, Stack the elements taken sequentially add to dst, if not to achieve a wildcard:

// PopAll method without wildcard type - deficient!
publicvoidpopAll (Collection < E > dst) {
    while (! isEmpty ())
        dst.add (pop ());
Similarly, suppose you have an instance of Stack < Number > object stack, dst for the Collection < Object >; popAll method call is type mismatch error occurs because the Collection < Object > is not a Collection < Number > subtype. Thus, it should read:

// Wildcard type for parameter that serves as an E consumer
publicvoidpopAll (Collection < ? super E > dst) {
    while (! isEmpty ())
        dst.add (pop ());
In the above example, when you call the method of producing the E pushAll instance (produces E instances), when calling popAll method dst consumed E examples (consumes E instances). Naftalin and Wadler will PECS referred to Get and Put Principle.

java.util.Collections the copy method (JDK1.7) perfect interpretation of the PECS:

public static < T > voidcopy (List < ? super T > dest, List < ? extends T > src) {
    int srcSize = src.size ();
    if (srcSize> dest.size ())
        throw new IndexOutOfBoundsException ( "Source does not fit in dest");

    if (srcSize < COPY_THRESHOLD ||
        (Src instanceof RandomAccess && dest instanceof RandomAccess)) {
        for (int i = 0; i             dest.set (i, src.get (i));
    } Else {
        ListIterator < ? Super T > di = dest.listIterator ();
        < ? Extends T > ListIterator si = src.listIterator ();
        for (int i = 0; i             di.next ();
            di.set (si.next ());
PECS Summary:

When data is taken from a generic class with extends;
To write data to a generic class with super;
Not only to take but also to write, do not have a wildcard (ie, extends and super do not).
- Create several practical points of high security PHP site (Linux)
- ASP.NET 5 tutorial series (Server)
- Running the open-source Swift under Linux platform (Linux)
- Linux installation JDK1.6 rpm.bin assembly (Linux)
- Realize screen recording and playback via Linux command (Linux)
- OpenSSH version smooth upgrade method (Linux)
- Linux input and output redirection (Linux)
- Java is simple to read and write HDFS Demo (Programming)
- Linux netstat command to get started (Linux)
- Cooling solutions Ubuntu system (Linux)
- The Linux role of each directory contents (Linux)
- Use small network command to check whether PC Security (Linux)
- The next key to install Linux bash script PowerShell (Linux)
- Linux Network Programming - raw socket can do (Programming)
- Use HugePages optimize memory performance (Database)
- Spark SQL job of a lifetime (Server)
- Open source backup software installed on Ubuntu Systemback 1.6.201 (Linux)
- Linux RPM default installation path (Linux)
- To help you easily protect the Linux System (Linux)
- JavaScript property of checkbox.disabled (Programming)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.