简体   繁体   English

Android Java是否支持泛型类型的基本类型参​​数?

[英]Does Android Java support primitive type parameter for generic types?

Today I was looking at the following piece of Android source code: 今天我看了下面这段Android源代码:

https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/animation/IntArrayEvaluator.java https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/animation/IntArrayEvaluator.java

The type is declared as: 该类型声明为:

public class IntArrayEvaluator implements TypeEvaluator<int[]>

My understanding of Java generics is that primitive types and arrays are not supported. 我对Java泛型的理解是不支持原始类型和数组。

Am I correct in that understanding? 我的理解是否正确?

If I am correct is this something unique to Android's implementation of Java? 如果我是正确的,这是Android的Java实现的独特之处吗?

Arrays are supported because internally one object will be created in Heap for all Primitive array as wrapper type. 支持数组,因为内部的一个对象将在Heap中为所有Primitive数组创建为包装类型。

but primitive types are not applicable. 但原始类型不适用。

No, you are not correct. 不,你不对。 int[] is allowed as a type parameter in both Android and standard Java. 允许int[]作为Android和标准Java中的类型参数。 This is because int[] is a reference type. 这是因为int[]是引用类型。 int , on the other hand, is a primitive type and is not allowed as a type parameter in either. 另一方面, int是基本类型,不允许作为类型参数。

int[] is a reference type, and so can be used as a generic type parameter; int[]是引用类型,因此可以用作泛型类型参数; this is not the same thing as the primitive int type. 这与原始int类型不同。 There is nothing peculiar to Android about this. 关于这一点,Android没有什么特别之处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM