java 数据类型默认值

作为局部变量必须初始化:


作为类成员不初始化时:

    char c;           // 输出1个空占位符,强制转换后(int)c 输出为0
    boolean bl;   // false
    
    byte bt;        // 0
    short s;         // 0
    int i;             // 0
    long l;          // 0
    
    float f;         // 0.0
    double d;    // 0.0
    
    String str;    // null

作为类成员数组时:【分为分配初始内存空间不分配初始内存空间两种情况】

    char[] cs ;                                      // 报错(若不输出,则不报错),

    char[] cs = new char[5] ;              //  输出5个空占位符,循环中强制转换(int)c后 输出为00000
    boolean[] bls ;                              // null,初始化后:[Z@33909752
    
    byte[] bts ;                                   // null,初始化后:[B@55f96302
    short[] ss;                                     // null,初始化后:[S@3d4eac69

    int[] is;                                          // null,初始化后:[I@42a57993

    long[] ls;                                       // null,初始化后:[J@75b84c92

   
    float[] fs;                                       // null,初始化后:[F@6bc7c054

    double[] ds;                                  // null,初始化后:[D@232204a1

   
    String[] strs;                                  // null,初始化后:[Ljava.lang.String;@4aa298b7



猜你喜欢

转载自blog.51cto.com/maplebb/2235406