java各類數據類型的數組元素的默認值

public class DataTypeDefaultValue {
    public static void main(String[] args) {
        // string類型數組的默認值null
        // 對於引用類型的屬性的默認值是null,如String類型
        System.out.println("查看String類型中數組的默認值:");
        String[] str = new String[4];
        str[0] = "aa";
        str[1] = "bb";
        str[2] = "cc";
        for (int i = 0; i < 4; i++) {
            System.out.println(str[i]);
        }
        // 對於short,int,long,byte而言,建立數組的默認值是0
        System.out.println("查看int類型數組的默認值:");
        int[] score = new int[4];
        score[0] = 90;
        score[1] = 89;
        score[2] = 34;
        for (int i = 0; i < score.length; i++) {
            System.out.println(score[i]);
        }
        System.out.println("查看short類型數組的默認值:");
        short[] score1 = new short[4];
        score1[0] = 90;
        for (int i = 0; i < score1.length; i++) {
            System.out.println(score1[i]);
        }
        System.out.println("查看long類型數組的默認值:");
        long[] score2 = new long[4];
        score2[0] = 90;
        for (int i = 0; i < score2.length; i++) {
            System.out.println(score2[i]);
        }
        System.out.println("查看byte類型數組的默認值:");
        byte[] score3 = new byte[4];
        score3[0] = 90;
        for (int i = 0; i < score3.length; i++) {
            System.out.println(score3[i]);
        }
        // 對於float double而言,默認值是0.0
        System.out.println("查看float類型數組的默認值:");
        float[] score4 = new float[4];
        score4[0] = 23;
        for (int i = 0; i < score4.length; i++) {
            System.out.println(score4[i]);
        }
        System.out.println("查看double類型數組的默認值:");
        double[] score5 = new double[4];
        score5[0] = 45;
        for (int i = 0; i < score5.length; i++) {
            System.out.println(score5[i]);
        }
        // 對於char類型
        // char類型數組的默認值是空格
        System.out.println("查看char類型數組的默認值:");
        char[] ch = new char[4];
        ch[0] = 'p';
        for (int i = 0; i < ch.length; i++) {
            System.out.println(ch[i]);
        }
        // 對於boolean類型的數組默認值
        // boolean類型數組的默認值是false
        System.out.println("查看boolean數組的默認值:");
        boolean[] b = new boolean[4];
        b[0] = true;
        for (int i = 0; i < b.length; i++) {
            System.out.println(b[i]);
        }
        /// 引用類型數組的默認值是null
        class Person {
        }
        System.out.println("查看引用類型的數組默認值:");
        Person[] p = new Person[4];
        for (int i = 0; i < p.length; i++) {
            System.out.println(p[i]);
        }
    }
}

運行結果:數組

查看String類型中數組的默認值:
aa
bb
cc
null
查看int類型數組的默認值:
90
89
34
0
查看short類型數組的默認值:
90
0
0
0
查看long類型數組的默認值:
90
0
0
0
查看byte類型數組的默認值:
90
0
0
0
查看float類型數組的默認值:
23.0
0.0
0.0
0.0
查看double類型數組的默認值:
45.0
0.0
0.0
0.0
查看char類型數組的默認值:
p
相關文章
相關標籤/搜索