Java是会为类的成员变量提供默认初始化的,但是又听说局部变量不会提供默认的初始化,是这样吗?
public class TestVariable { public int id; public String name; public char c; public byte by; public float f; public double d; public boolean bo; // 再次说明了局部变量只能有final修饰符,static都不行 @Test public void testV(){ int no; final int no2; System.out.println(id); // 0 System.out.println(name); // null System.out.println(c); // 空字符 System.out.println(by); // 0 System.out.println(f); // 0.0 System.out.println(d); // 0.0 System.out.println(bo); // false // 局部变量没有初始化,编译就报错 // System.out.println(no); // System.out.println(no2); } }结果男默女泪,编译时就出错了
结论是会为类的普通属性成员初始化
String: null Other Object: null
局部变量并不会默认的初始化,而是在编译时就直接报错!!!
局部变量不允许使用修饰符,只能使用final,static都不行
