java中所有的变量必须先声明,后赋值才能使用
java中的成员变量,在创建对象的时候,都会执行一次初始化操作,都会给一个默认值
public class Child {
}
public class Person {
byte b;
short s;
int i;
long l;
float f;
double d;
boolean bool;
char c;
String str;//String是java提供的一个类 ,null
Child child;//自己写的类 null
}
public class Test {
public static void main(String[] args) {
Person p=new Person();
System.out.println("byte="+p.b);
System.out.println("short="+p.s);
System.out.println("int="+p.i);
System.out.println("long="+p.l);
System.out.println("float="+p.f);
System.out.println("double="+p.d);
System.out.println("boolean="+p.bool);
System.out.println("char="+(int)p.c);
System.out.println("str="+p.str);
System.out.println("child="+p.child);
}
}
运行结果:
byte=0
short=0
int=0
long=0
float=0.0
double=0.0
boolean=false
char=0
str=null
child=null
Process finished with exit code 0
基本数据类型默认值都是 0 包括boolean->false
引用数据类型:null
null表示空,什么都没有,占位
(自主学习,侵权可删)