天天看點

數組元素的預設初始化值

對于基本資料類型的變量建立的數組:byte,short,int,long,float,double,char,boolean:

1.對于byte,short,long,int而言:建立數組以後預設值為0

package com;

public class V {
    public static void main(String[] args){
        int[] a=new int[3];//定義從0開始的三個數字的數組
        a[0]=70;
        a[2]=90;
        for(int i=0;i<a.length;i++){
            System.out.println(a[i]);
        }
    }
}      
數組元素的預設初始化值

2.對于double,float而言:預設值為0.0

public class V {
    public static void main(String[] args){
        float[] a=new float[3];//定義從0開始的三個數字的數組
        a[0]=70F;
        a[2]=90F;
        for(int i=0;i<a.length;i++){
            System.out.println(a[i]);
        }
    }
}      
數組元素的預設初始化值

3.對于char類型而言:預設值為空

public class V {
    public static void main(String[] args){
        char[] a=new char[3];//定義從0開始的三個數組
        a[0]='男';
        a[2]='女';
        for(int i=0;i<a.length;i++){
            System.out.println(a[i]);
        }
    }
}      
數組元素的預設初始化值

4.對于Boolean而言:預設值為false

public class V {
    public static void main(String[] args){
        boolean[] a=new boolean[3];//定義從0開始的三個數組
        a[0]=true;
        a[2]=false;
        for(int i=0;i<a.length;i++){
            System.out.println(a[i]);
        }
    }
}      
數組元素的預設初始化值

5.對于引用類型的變量構成的數組而言:預設初始化值為null

public class V {
    public static void main(String[] args){
        String[] a=new String[3];//定義從0開始的三個數組
        a[0]="陽陽";
        a[2]="寅寅";
        for(int i=0;i<a.length;i++){
            System.out.println(a[i]);
        }
    }
}      
數組元素的預設初始化值

轉載于:https://www.cnblogs.com/KeepCalmAndNeverSayNever/p/10099805.html