简体   繁体   English

数组是原始类型还是对象(或其他完全类型)?

[英]Is an array a primitive type or an object (or something else entirely)?

The question is basically self-explanatory.这个问题基本上是不言自明的。 I haven't been able to find an API for arrays (other than this Arrays , but this just defines a bunch of static helper functions for dealing with actual arrays).我一直无法找到数组的 API(除了这个Arrays ,但这只是定义了一堆用于处理实际数组的静态辅助函数)。 If there is no class for it, this seems to suggest that an array can't be an Object .如果没有它的类,这似乎表明数组不能是Object

However, the fact that an array has public fields like length and methods that it can invoke like .equals() and .clone() seem to suggest (very strongly) the complete opposite.然而,数组具有像length这样的公共字段和可以像.equals().clone()这样调用的方法这一事实似乎(非常强烈地)暗示了完全相反的情况。

What is the explanation for the odd presentation and behavior of primitive arrays?对原始数组的奇怪表示和行为的解释是什么?

As a note, I tried to use the "Open Implementation" Eclipse feature on the .clone() method of an array just now, hoping that I would be able to look at where and how this method was defined (since it said int[] overrode it from Object), but it actually caused my entire Eclipse to freeze up and crash...作为说明,我刚刚.clone()在数组的.clone()方法上使用“开放实现”Eclipse 功能,希望我能够查看此方法的定义位置和方式(因为它说 int[ ] 从对象覆盖它),但它实际上导致我的整个 Eclipse 冻结并崩溃......

There is a class for every array type, so there's a class for int[] , there's a class for Foo[] .每个数组类型都有一个类,所以有一个用于int[]的类,有一个用于Foo[]的类。 These classes are created by JVM.这些类是由 JVM 创建的。 You can access them by int[].class , Foo[].class .您可以通过int[].classFoo[].class访问它们。 The direct super class of these classes are Object.class这些类的直接超类是Object.class

public static void main(String[] args)
{
    test(int[].class);
    test(String[].class);
}

static void test(Class clazz)
{
    System.out.println(clazz.getName());
    System.out.println(clazz.getSuperclass());
    for(Class face : clazz.getInterfaces())
        System.out.println(face);
}

There's also a compile-time subtyping rule, if A is subtype of B , A[] is subtype of B[] .还有一个编译时子类型规则,如果AB子类型,则A[]B[]子类型。

The Java Language Specification should give you an idea:Java 语言规范应该给你一个想法:

The direct superclass of an array type is Object.数组类型的直接超类是 Object。

Every array type implements the interfaces Cloneable and java.io.Serializable .每个数组类型都实现了接口Cloneablejava.io.Serializable

Moreover : 此外

An object is a class instance or an array.一个对象是一个类实例或一个数组。

So arrays are not instances and therefore you don't need a constructor to create them.因此数组不是实例,因此您不需要构造函数来创建它们。 Instead you use the Array Creation Expressions .相反,您使用Array Creation Expressions

See the below code.请参阅下面的代码。 It compiles:它编译:

    int[] arr = new int[2];
    System.out.println(arr.toString());

Now, on any primitive type, you cannot call a method( toString() ) defined in Object class (Or, any method for that matter)... So, an array is essentially an Object .现在,在任何原始类型上,您不能调用 Object 类中定义的方法( toString() )(或者,任何与此相关的方法)......因此,数组本质上是一个Object

OK, here you go:好的,给你:

From the JLS Section 4.3 :来自JLS 第 4.3 节

There are four kinds of reference types: class types (§8), interface types (§9), type variables (§4.4), and array types (§10).有四种引用类型:类类型(第 8 节)、接口类型(第 9 节)、类型变量(第 4.4 节)和数组类型(第 10 节)。

And, Section 10 :并且,第 10 节

In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2).在 Java 编程语言中,数组是对象(第 4.3.1 节),是动态创建的,并且可以分配给对象类型的变量(第 4.3.2 节)。 All methods of class Object may be invoked on an array. Object 类的所有方法都可以在数组上调用。

So, from the first quote, Array is not actually a class... It is another type.所以,从第一句话开始, Array实际上不是一个类……它是另一种类型。 But, essentially arrays are objects, though not of some Class , but they are of Array type.但是,本质上数组是对象,虽然不是某些Class ,但它们是Array类型。 So they are not instances of some class, and may be objects of array are defined to be created that way...所以它们不是某个类的实例,可能是array对象被定义为以这种方式创建......

So short and simple, yes <Type>[] is a type of Object .如此简短,是的 <Type>[] 是一种Object It extends directly from Object as I understand it.据我所知,它直接从Object扩展。 There are all the Object methods on it, toString() , hashCode() , ... Plus a special exposed variable called length .上面有所有的 Object 方法, toString()hashCode() ,...加上一个特殊的暴露变量length The class java.util.Arrays is a utility class for dealing with types of Arrays.java.util.Arrays是用于处理数组类型的实用程序类。 It's a little confusing when you add to the mess things like: int[] does not inherit from Object[] .当您添加诸如int[]不继承自Object[]类的东西时,这有点令人困惑。 Also, unlike other Object types, there are no constructors for array types.此外,与其他Object类型不同,数组类型没有构造函数。 They respect the new keyword but that is usually to allocate for the size.他们尊重new关键字,但这通常是分配大小。 It's a little bizarre, but just one of those language quirks.这有点奇怪,但这只是这些语言怪癖之一。

To answer the question though, yes they are an object.不过,要回答这个问题,是的,它们是一个对象。

An array is a container object that holds a fixed number of values of a single type.数组是一个容器对象,它包含固定数量的单一类型的值。

See http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html请参阅http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Only those several primitive types in Java as we known.正如我们所知,只有 Java 中的那几种原始类型。 Basically, we still have several steps to create an array, such as declare, construct or initialize if needed, and that means array is an object indeed.基本上,我们仍然有几个步骤来创建数组,例如声明、构造或初始化(如果需要),这意味着数组确实是一个对象。

Stepping deeper, the primitive types could be stored in memory with the original values but object is an address(reference).更深入地讲,原始类型可以与原始值一起存储在内存中,但对象是地址(引用)。 So we can imagine a paradox, how could we store the original values in the memory if the array is an primitive type?所以我们可以想象一个悖论,如果数组是原始类型,我们如何将原始值存储在内存中? I think the same as String, but String is a final object so that you can construct an object in an easy way, String s = "s", like a primitive type.我认为与 String 相同,但 String 是最终对象,因此您可以以简单的方式构造对象,String s = "s",就像原始类型一样。

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

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