基本类型的初始化分两种情况:方法内部的局部变量、类的成员变量
方法内部的局部变量
public class BasicTypeInit {
void f(){
int i ;
System.out.println(i++);//在未赋值的情况下使用会报错
}
}
类的成员变量
public class BasicTypeInit {
byte b;
char c;
boolean flag;
int j;
public static void main(String[] args) {
BasicTypeInit basicTypeInit = new BasicTypeInit();
basicTypeInit.j++;
basicTypeInit.j++;
System.out.println(basicTypeInit.j);
}
}
指定初始化(在强大编辑器的加持下,该问题都不是问题)
//注意:非法的向前调用
public class MethodInit3 {
//int j = g(i); // Illegal forward reference
int i = f();
int f() { return 11; }
int g(int n) { return n * 10; }
}