java数组各种数据类型的默认初始化值

public class TestArray {
public static void main(String[] args) {
// 对于基本数据类型的变量创建的数组:byte short int long float double char boolean
// 对于byte short int long 而言:创建数组以后,默认值为0
int[] scores = new int[4];
scores[0] = 89;
scores[3] = 90;
for(int i = 0;i < scores.length;i++) {
System.out.println(scores[i]);
}
byte[] scores1 = new byte[4];
scores1[0] = 89;
scores1[3] = 90;
for(int i = 0;i < scores1.length;i++) {
System.out.println(scores1[i]);
}
// 2.对于float double 而言,默认值为0.0
float[] f = new float[3];
f[0] = 1.2F;
for(int i = 0;i < f.length;i++) {
System.out.println(f[i]);
}
// 3.对于char而言:默认空格
char[] c = new char[3];
for(int i = 0;i < c.length;i++) {
System.out.println(c[i]);
}
// 4.对于boolean而言,默认为flase
boolean[] b = new boolean[3];
for(int i = 0;i < b.length;i++) {
System.out.println(b[i]);
}
// 5.对于引用类型的变量构成的数组而言:默认初始化值为null.以String为例
String[] strs = new String[4];
strs[0] = “AA”;
strs[1] = “BB”;
strs[2] = “CC”;
strs[3] = “DD”;
// 遍历数组元素
for(int i = 0;i < strs.length;i++) {
System.out.println(strs[i]);
}
Person[] pers = new Person[3];
for(int i = 0;i < pers.length;i++) {
System.out.println(pers[i]);
}

}

}
class Person{

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值