Java几种类型数组的默认值

无论是C语言还是Java都必不可少的使用到数组。

说起数组,就不得不说说数组的默认值。

之前一直不太明白这些的默认值到底是什么?碰到了索性都编了一下还是用事实说话,比较有利。

实验发现:

1、int类型定义的数组,初始化默认是0

2、String类型定义的数组,默认值是null

3、char类型定义的数组,使用UTF8字符集 给出的结果是 

4、double类型定义的数组,默认值是0.0

5、float类型定义的数组,默认值是0.0

6、boolean类型定义的数组,默认值是false

而且不仅仅是数组第一个元素有默认值,所有的数组的默认值和上面的规则一样

实验代码:

package com.xaut.cherry.niukewang0702;
/*
 * 几种类型的数组的默认值
 * */
public class ArrayInitialValue {


public static void main(String[] args) {

int [] intarray = new int [2];
for(int i = 0;i<intarray.length;i++){
    System.out.println("int : "+intarray[i]); //int类型定义的数组,初始化默认是0
}
System.out.println();
String [] stringarray = new String[2];
for(int i = 0;i<stringarray.length;i++){
    System.out.println("String : "+stringarray[i]);   //String类型定义的数组,默认值是null
}
System.out.println();
char [] chararray = new char[2];
for(int i = 0;i<chararray.length;i++){
    System.out.println("char : "+(int)chararray[i]);  //char类型定义的数组,默认值是0对应的字符
}
System.out.println();
double [] doublearray = new double[2];
for(int i = 0;i<doublearray.length;i++){
    System.out.println("double : "+doublearray[i]); //double类型定义的数组,默认值是0.0
}
System.out.println();
float [] floatarray = new float[2];
for(int i = 0;i<floatarray.length;i++){
    System.out.println("float : "+floatarray[i]); //float类型定义的数组,默认值是0.0
}
boolean [] booleans = new boolean[2];
for(int i = 0;i<booleans.length;i++){
    System.out.println("float : "+booleans[i]); //float类型定义的数组,默认值是0.0
}



}


}

实验结果:

 

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值