java中数组是对象吗?

数组是对象哦。数组的父类也是Object,每个数组都实现了接口Cloneable and java.io.Serializable。但是数组的类型却不是程序员可见的。但是由于数组也是Object的子类,我们可以打印出数据的类型名。

// Java program to display class of 
// int array type
public class Test
{
    public static void main(String[] args)
    {
        int[] x = new int[3];
        System.out.println(x.getClass().getName());
    }
}

输出:

[I 

“[”表示这是一个数组,而且是一维的,“I”表示数组元素是int类型的。

通过这个方法还可以打印其他类型的数组名来:

Array type             Corresponding class Name
int[]                     [I
int[][]                   [[I
double[]                  [D
double[][]                [[D
short[]                   [S
byte[]                    [B
boolean[]                 [Z

over~ over~

参考:

https://www.geeksforgeeks.org/array-primitive-type-object-java/

猜你喜欢

转载自blog.csdn.net/jdbdh/article/details/82117952
今日推荐