Java各个变量默认值

package newqueastion;

public class NewQuestion {
    byte b;
    short s;
    int i;
    long l;
    float f;
    double d;
    int[] arr=null;
    int[] arr1;
    boolean boo;
    char c;
    String str;
    public boolean testMethon(){
        return true;
    }
}

package newqueastion;

public class Test {
    public static void main(String[] args) {
        //正确的实例化方式 打印true 0 null
        NewQuestion c=new NewQuestion();
        System.out.println(c.testMethon()+"\t");
        //成员变量不写会有默认值

        System.out.println("------默认值测试------");
        //byte、short、int、long默认值为0
        System.out.println(c.b);
        System.out.println(c.s);
        System.out.println(c.i);
        System.out.println(c.l);
        //float、double默认值为0.0
        System.out.println(c.f);
        System.out.println(c.d);
        //数组默认null
        System.out.println(c.arr);
        System.out.println(c.arr1);
        //boolean 为false
        System.out.println(c.boo);
        //char类型 \u0000
        System.out.println(c.c);
        //String类型 null
        System.out.println(c.str);

    }
}

运行结果图片


版权声明:本文为qq_42788363原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。