【Java】数组的类型

数组是一种引用类型

想要证明一个对象是引用类型的变量,找到它的类即可。

虽然我们找不到Java里任何数组的类的实现(注意java.util.Arrays只是数组的工具类,并不是数组的类),但我们可以用反射来证实:

int[] int_array = new int[10];
System.out.println("int[]的类型是:" + int_array.getClass());

结果是:
int[]的类型是:class [I

我们知道,Java里面所有的类都有公有的父类:java.lang.Object,int[]的类型应该是java.lang.Object类型的派生类。

我们用反射先获取int[]的类型再获取其基类类型:

int[] int_array = new int[10];
System.out.println("int[]的类型是:" + int_array.getClass());
System.out.println("int[]的父类:" + int_array.getClass().getSuperclass());

结果是:
int[]的类型是:class [I
int[]的父类:class java.lang.Object

数组的类型名称

基本类型元素的数组

获取基本类型元素的数组类型名称:

long[] long_array = new long[10];
int[] int_array = new int[10];
short[] short_array = new short[10];
byte[] byte_array = new byte[10];
char[] char_array = new char[10];
double[] double_array = new double[10];
float[] float_array = new float[10];
boolean[] boolean_array = new boolean[10];
System.out.println("long[]的类型是:" + long_array.getClass());
System.out.println("int[]的类型是:" + int_array.getClass());
System.out.println("short[]的类型是:" + short_array.getClass());
System.out.println("byte[]的类型是:" + byte_array.getClass());
System.out.println("char[]的类型是:" + char_array.getClass());
System.out.println("double[]的类型是:" + double_array.getClass());
System.out.println("float[]的类型是:" + float_array.getClass());
System.out.println("boolean[]的类型是:" + boolean_array.getClass());

输出结果:
long[]的类型是:class [J
int[]的类型是:class [I
short[]的类型是:class [S
byte[]的类型是:class [B
char[]的类型是:class [C
double[]的类型是:class [D
float[]的类型是:class [F
boolean[]的类型是:class [Z

引用类型元素的数组

获取引用类型元素的数组类型名称:

Integer[] integer_array = new Integer[10];
Boolean[] boolean_b_array = new Boolean[10];
String[] string_array = new String[10];
Arrays[] arrays_array = new Arrays[10];
BigInteger[] bigInteger_array = new BigInteger[10];
System.out.println("Integer[]的类型是:" + integer_array.getClass());
System.out.println("Boolean[]的类型是:" + boolean_b_array.getClass());
System.out.println("String[]的类型是:" + string_array.getClass());
System.out.println("Arrays[]的类型是:" + arrays_array.getClass());
System.out.println("BigInteger[]的类型是:" + bigInteger_array.getClass());

输出结果:
Integer[]的类型是:class [Ljava.lang.Integer;
Boolean[]的类型是:class [Ljava.lang.Boolean;
String[]的类型是:class [Ljava.lang.String;
Arrays[]的类型是:class [Ljava.util.Arrays;
BigInteger[]的类型是:class [Ljava.math.BigInteger;

二维数组与多维数组

获取二维数组与多维数组的类型名称:

int[][] int_int_array = new int[10][10];
int[][][] int_int_int_array = new int[10][10][10];
System.out.println("int[][]的类型是:" + int_int_array.getClass());
System.out.println("int[][][]的类型是:" + int_int_int_array.getClass());

输出结果:
int[][]的类型是:class [[I
int[][][]的类型是:class [[[I

完整代码

import java.math.BigInteger;
import java.util.Arrays;

public class ArrayTypeDemo {
    public static void main(String[] args) {
        long[] long_array = new long[10];
        int[] int_array = new int[10];
        short[] short_array = new short[10];
        byte[] byte_array = new byte[10];
        char[] char_array = new char[10];
        double[] double_array = new double[10];
        float[] float_array = new float[10];
        boolean[] boolean_array = new boolean[10];
        // 包装类的数组
        Integer[] integer_array = new Integer[10];
        Boolean[] boolean_b_array = new Boolean[10];
        // 某些引用类型的数组
        String[] string_array = new String[10];
        Arrays[] arrays_array = new Arrays[10];
        BigInteger[] bigInteger_array = new BigInteger[10];
        // 二维数组
        int[][] int_int_array = new int[10][10];
        // 三维数组
        int[][][] int_int_int_array = new int[10][10][10];
        System.out.println("long[]的类型是:" + long_array.getClass());
        System.out.println("int[]的类型是:" + int_array.getClass());
        System.out.println("short[]的类型是:" + short_array.getClass());
        System.out.println("byte[]的类型是:" + byte_array.getClass());
        System.out.println("char[]的类型是:" + char_array.getClass());
        System.out.println("double[]的类型是:" + double_array.getClass());
        System.out.println("float[]的类型是:" + float_array.getClass());
        System.out.println("boolean[]的类型是:" + boolean_array.getClass());
        System.out.println("Integer[]的类型是:" + integer_array.getClass());
        System.out.println("Boolean[]的类型是:" + boolean_b_array.getClass());
        System.out.println("String[]的类型是:" + string_array.getClass());
        System.out.println("Arrays[]的类型是:" + arrays_array.getClass());
        System.out.println("BigInteger[]的类型是:" + bigInteger_array.getClass());
        System.out.println("int[][]的类型是:" + int_int_array.getClass());
        System.out.println("int[][][]的类型是:" + int_int_int_array.getClass());
        // 父类
        System.out.println("int[]的父类:" + int_array.getClass().getSuperclass());
    }
}

输出结果:

long[]的类型是:class [J
int[]的类型是:class [I
short[]的类型是:class [S
byte[]的类型是:class [B
char[]的类型是:class [C
double[]的类型是:class [D
float[]的类型是:class [F
boolean[]的类型是:class [Z
Integer[]的类型是:class [Ljava.lang.Integer;
Boolean[]的类型是:class [Ljava.lang.Boolean;
String[]的类型是:class [Ljava.lang.String;
Arrays[]的类型是:class [Ljava.util.Arrays;
BigInteger[]的类型是:class [Ljava.math.BigInteger;
int[][]的类型是:class [[I
int[][][]的类型是:class [[[I
int[]的父类:class java.lang.Object

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星拱北辰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值