8种基本数据类型数组的默认值

更多精彩文章欢迎关注公众号“Java之康庄大道”

总结:

引用数据类型的默认值是null

例如String类型数组默认值为null

也可以创建一个类来引用

class person{}
person[] _per=new person[3];
for(int i=0;i<_per.length;i++){
    System.out.println(_per[i]);
}

以此法创建的person类声明的数组的默认值也是null

运行结果会是:

null

null

null

8中基本数据类型:

①byte short int long 这四种基本数据类型数组默认值为0

②float double 这两种数组默认值是0.0

③char这种类型数组默认值为空格

④boolean类型数组默认值为false

 1 package com.baidu.java;
 2 
 3 public class TestArrayMRZ { //快捷提示 Alt+/ 按键
 4     public static void main(String[] args) {
 5         String[] strs=new String[3];//声明String类型数组strs
 6         strs[0]="java";
 7         //strs[1]="PHP";//由此可以看出String类型数组的默认值是null
 8         strs[2]=".Net";
 9         System.out.println("由此可以看出String类型的数组默认值为null");
10         for(int i=0;i<strs.length;i++){
11             System.out.println(strs[i]);
12         }
13         //8种基本数据类型的数组默认值
14         //byte short int long float double char boolean
15         //1.byte short int long类型
16         int[] _int=new int[3];//声明int型数组_int
17         _int[0]=9;
18         System.out.println("int byte short long类型数组的默认值是0");
19         for(int i=0;i<_int.length;i++){
20             System.out.println(_int[i]);
21         }
22         //2.float double类型
23         float[] _float=new float[3];//声明float类型数组
24         System.out.println("float double类型数组的默认值是0.0");
25         for(int i=0;i<_float.length;i++){
26             System.out.println(_float[i]);
27         }
28         //3.char类型
29         char[] _char=new char[3];
30         System.out.println("char类型数组的默认值为空格");
31         for(int i=0;i<_char.length;i++){
32             System.out.println(_char[i]);
33         }
34         //4.boolean类型
35         boolean _bool[]=new boolean[3];
36         //_bool[0]=true;
37         System.out.println("boolean类型的数组默认值为false");
38         for(int i=0;i<_bool.length;i++){
39             System.out.println(_bool[i]);
40         }
41         
42     }
43 }

运行结果:

原文地址:https://www.cnblogs.com/yunqing/p/4736127.html