


Introduction to the knowledge of Java array covariance and generic invariance (with code)
What this article brings to you is an introduction to the knowledge of Java array covariance and generic invariance (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.
Variability is a big pitfall of OOP language invariance, and Java's array covariance is one of the old pitfalls. Because I stepped on it recently, I made a note. By the way, let’s also mention the degeneration of paradigms.
Before explaining array covariance, first clarify three related concepts, covariance, invariance and contravariance.
1. Covariance, invariance, contravariance
Suppose, I wrote such a piece of code for a restaurant
class Soup<T> { public void add(T t) {} } class Vegetable { } class Carrot extends Vegetable { }
There is a generic class Soup
Then the question is, what is the relationship between Soup
The first reaction is that Soup
Soup<Vegetable> soup = new Soup<Carrot>(); soup.add(new Tomato());
The first sentence is okay, Soup
However, there is a problem when putting the two sentences together. The actual type of soup is Soup
So, what is the relationship between Soup
(1) If Soup
(2) If Soup
(3) If Soup
After understanding the concepts of covariance, invariance and contravariance, let’s look at the implementation of Java. Java's general generics are immutable, which means Soup
2. Array covariance
In Java, arrays are basic types, not generics, and there is no such thing as Array
Different from the immutability of generics, Java arrays are covariant. In other words, Carrot[] is a subclass of Vegetable[]. The examples in the previous section have shown that covariance can sometimes cause problems. For example, the following code
Vegetable[] vegetables = new Carrot[10]; vegetables[0] = new Tomato(); // 运行期错误
Because arrays are covariant, the compiler allows Carrot[10] to be assigned to variables of type Vegetable[], so this The code can be compiled successfully. It's only during runtime, when the JVM actually tries to insert a tomato into a pile of carrots, that something big goes wrong. Therefore, the above code will throw an exception of type java.lang.ArrayStoreException during runtime.
Array covariance is one of Java’s famous historical baggage. Be careful when using arrays!
If you replace the array in the example with a List, the situation will be different. Like this
ArrayList<Vegetable> vegetables = new ArrayList<Carrot>(); // 编译期错误 vegetables.add(new Tomato());
ArrayList is a generic class and it is immutable. Therefore, there is no inheritance relationship between ArrayList
Although both pieces of code will report errors, compile-time errors are usually easier to handle than run-time errors.
3. When generics also want covariance and contravariance
Generics are immutable, but in some scenarios we Still hope it can covariate. For example, there is a young lady who drinks vegetable soup every day to lose weight
class Girl { public void drink(Soup<Vegetable> soup) {} }
我们希望drink方法可以接受各种不同的蔬菜汤,包括Soup
要实现这一点,应该采用一种类似于协变性的写法
public void drink(Soup<? extends Vegetable> soup) {}
意思是,参数soup的类型是泛型类Soup
但是,这种方法有一个限制。编译器只知道泛型参数是Vegetable的子类,却不知道它具体是什么。所以,所有非null的泛型类型参数均被视为不安全的。说起来很拗口,其实很简单。直接上代码
public void drink(Soup<? extends Vegetable> soup) { soup.add(new Tomato()); // 错误 soup.add(null); // 正确}
方法内的第一句会在编译期报错。因为编译器只知道add方法的参数是Vegetable的子类,却不知道它具体是Carrot、Tomato、或者其他的什么类型。这时,传递一个具体类型的实例一律被视为不安全的。即使soup真的是Soup
但是方法内的第二句是正确的。因为参数是null,它可以是任何合法的类型。编译器认为它是安全的。
同样,也有一种类似于逆变的方法
public void drink(Soup<? super Vegetable> soup) {}
这时,Soup
这种情况就不存在上面的限制了,下面的代码毫无问题
public void drink(Soup<? super Vegetable> soup) { soup.add(new Tomato()); }
Tomato是Vegetable的子类,自然也是Vegetable父类的子类。所以,编译期就可以确定类型是安全的。
The above is the detailed content of Introduction to the knowledge of Java array covariance and generic invariance (with code). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Five efficient Java array deduplication methods revealed In the Java development process, we often encounter situations where we need to deduplicate arrays. Deduplication is to remove duplicate elements in an array and keep only one. This article will introduce five efficient Java array deduplication methods and provide specific code examples. Method 1: Use HashSet to deduplicate HashSet is an unordered, non-duplicate collection that automatically deduplicates when adding elements. Therefore, we can use the characteristics of HashSet to deduplicate arrays. public

Common ways to add elements to Java arrays, specific code examples are required In Java, an array is a common data structure that can store multiple elements of the same type. In actual development, we often need to add new elements to the array. This article will introduce common methods of adding elements to arrays in Java and provide specific code examples. A simple way to create a new array using a loop is to create a new array, copy the elements of the old array into the new array, and add the new elements. The code example is as follows: //original array i

Commonly used methods include length attribute, copy array, array traversal, array sorting, array conversion to string, etc. Detailed introduction: 1. Length attribute: used to get the length of an array. It is an attribute rather than a method. Example: int[] arr = {1, 2, 3}; int length = arr.length;; 2. Copy the array: Use the System.arraycopy() method or the copyOf() method of the Arrays class to copy the contents of the array to a new Arrays etc.

Detailed explanation of five classic Java array deduplication algorithms In Java programming, you often encounter situations where you need to perform deduplication operations on arrays, that is, remove duplicate elements in the array and retain unique elements. The following will introduce five classic Java array deduplication algorithms and provide corresponding code examples. Using HashSet HashSet is a collection class in Java that automatically removes duplicate elements. This feature can be used to quickly achieve array deduplication. Code example: importjava.util.Arr

Java is a widely used programming language that provides programmers with many practical and powerful tools and features. When writing Java programs, you may encounter various exceptions. Among them, ArrayIndexOutOfBoundsException is a common exception. This exception is triggered when we try to access an element that does not exist in the array. In this article, we will discuss ArrayIndexOutOfBoundsExc in Java in detail

How to use arrays and collections for data storage and operation in Java In Java programming, arrays and collections are commonly used methods of data storage and operation. An array is a container used to store data of the same type, while a collection is an object composed of multiple elements. The basic method of using arrays for data storage and manipulation is as follows: Declaring an array variable To use an array, you first need to declare an array variable. An array variable can be declared using the following syntax: dataType[]arrayName; where dataT

An in-depth analysis of five practical methods for deduplicating Java arrays. In Java, processing arrays is a very common operation. Array deduplication is a problem often encountered in actual development. This article will provide an in-depth analysis of five practical methods for Java array deduplication and provide specific code examples. 1. Use HashSet to remove duplicates. HashSet is a collection in Java that has the function of automatic deduplication. We can use the characteristics of HashSet to add elements in the array to HashSet to achieve the effect of deduplication.

Tips and precautions for adding elements to arrays in Java In Java, arrays are a very common and important data structure. It can store a set of elements of the same type, and these elements can be accessed and modified through indexes. In practical applications, we often need to dynamically add elements to an array. This article will introduce some tips and precautions for adding elements to arrays in Java, and provide corresponding code examples. Use dynamic array (ArrayList) to add elements Dynamic array ArrayList is
