數組的默認值


package com.kk.java;

public class TestArray1 {
public static void main(String[] args) {

//對於基於基本數據類型的變量創建的數組:byte short int long float double char boolean
//1、對於byte short int long而言:創建數組后,元素默認為0
int[] scores = new int[4];
scores[0] = 77;
scores[3] = 88;
for(int i = 0;i < scores.length;i++) {
System.out.println(scores[i]);

}
//2、對於float double而言:默認值為0.0
float[] f = new float[3];
for(int i = 0;i < f.length;i++) {
System.out.println(f[i]);

}
//3、對於char而言:默認為空格
char[] c = new char[3];
c[2] = 'd';
for(int i = 0;i < c.length;i++) {
System.out.println(c[i]);

}
//4、對於boolean而言:默認為false
//5、對於引用類型的變量構成的數組:默認初始化值為null
String[] strs = new String[4];
strs[0] = "AA";
strs[1] = "BB";
strs[3] = "DD";
for(int i = 0;i < strs.length;i++) {
System.out.println(strs[i]);

}
}

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM