Java未赋值成员变量的初始值(默认值)
java中的所有变量必须先声明,后赋值才能使用。
java中的成员变量,在创建对象的时候,都会执行一次初始化操作,都会给一个默认值。
基本数据类型默认值都是0,包括boolean->false
引用数据类型:null
null表示空,什么都没有,只是占位。
char类型实际也是0,只不过0是空字符即'\u0000'
数据类型 | 关键字 | 内存占用 | 成员变量初始值 | 取值范围 |
---|---|---|---|---|
整数类型 | byte | 1 | -128~127 | |
short | 2 | -32768~32767 | ||
int (默认) | 4 | 0 | -2的31次方到2的31次方-1 | |
long | 8 | 0 | -2的63次方到2的63次方-1 | |
浮点类型 | float | 4 | 0.0 | 负数:-3.402823E+38到-1.401298E-45 正数: 1.401298E-45到3.402823E+38 |
double (默 认) | 8 | 0.0 | 负数:-1.797693E+308到-4.9000000E-324 正数:4.9000000E-324 到 1.797693E+308 | |
字符类型 | char | 2 | 'u0000' | 0-65535 |
布尔类型 | boolean | 1 | false | true,false |
引用类型 | String | null |
代码
public class test1 {
public byte a;
public short b;
public int c;
public long d;
public char e;
public double f;
public float g;
public String h;
public test1 i;
public static void main(String[] args) {
test1 x=new test1();
System.out.println("byte="+x.a);
System.out.println("short="+x.b);
System.out.println("int="+x.c);
System.out.println("long="+x.d);
System.out.println("char="+x.e);
System.out.println("double="+x.h);
System.out.println("float="+x.g);
//对象和String都是引用类型
System.out.println("String="+x.h);
System.out.println("test1="+x.i);
}
}
IDEA运行结果
IDEA调试看字符类型初始值