是一个数组基本类型或对象(或别的东西完全)? [英] Is an array a primitive type or an object (or something else entirely)?

查看:148
本文介绍了是一个数组基本类型或对象(或别的东西完全)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的问题基本上是不言自明的。我一直没能找到阵列(其他比这阵列的API ,但这只是定义了一组静态辅助函数来处理实际阵列)。如果没有类吧,这似乎表明,数组不能是一个对象

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). If there is no class for it, this seems to suggest that an array can't be an Object.

然而,一个数组有一个像长度和方法,它可以调用像 .equals公共字段()的事实 .clone()似乎暗示(强烈)完全相反的。

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.

什么是基本数组的奇数presentation和行为的解释?

What is the explanation for the odd presentation and behavior of primitive arrays?

作为一个说明,我想()数组的方法,刚才用了开放实现Eclipse功能上的 .clone,希望我能看哪里,这种方法是如何定义的(因为它说,INT []推翻它从Object),但它实际上使我的整个Eclipse冻结和崩溃...

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...

推荐答案

有对每个数组类型的类,所以有一类为 INT [] ,有一个类富[] 。这些类是由JVM创建。您可以通过 INT []访问它们。类富[]。类。直接超类,这些类都是 Object.class

There is a class for every array type, so there's a class for int[], there's a class for Foo[]. These classes are created by JVM. You can access them by int[].class, Foo[].class. The direct super class of these classes are 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);
}

还有一个编译时子类型的规则,如果 A B 的亚型, A [] 乙:[]

这篇关于是一个数组基本类型或对象(或别的东西完全)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆