Java类成员变量的默认值

1、布尔型(boolean)变量默认值为false,byte、short、int、long为0,字符型为'u0000'(空字符),浮点型(float double)为0.0,引用类型(String)为null。

 1 package cn.nxl2018;
 2 public class Test {
 3     private boolean bool;
 4     private byte bt;
 5     private short st;
 6     private char ch;
 7     private int  i;
 8     private long l;
 9     private float f;
10     private double d;
11     private String str;
12     public static void main(String[] args){
13         int j;
14         Test test=new Test();
15         System.out.println(test.bool);//默认值是false
16         System.out.println(test.bt);//默认值是0
17         System.out.println(test.st);//默认值是0
18         System.out.println(test.ch);//默认值是空字符('u0000')
19         System.out.println(test.i);//默认是0
20         System.out.println(test.l);//默认是0
21         System.out.println(test.f);//默认是0.0
22         System.out.println(test.d);//默认是0.0
23         System.out.println(test.str);//默认是null
24         //System.out.println(j);//The local variable j may not have been initialized(变量可能没有被初始化)
25     }
26 }

2、注意:未初始化的局部变量是不可以使用的,在这里可以认为是因为局部变量没有默认值,所以不可以直接使用。空字符('u0000')什么也不输出,不要认为输出是空格。

关联博客(SCDN):https://blog.csdn.net/m0_38022608/article/details/80209741

 
原文地址:https://www.cnblogs.com/qikeyishu/p/8996259.html