public class Test {
public static void main(String[] args) {
int i;
try {
i=9;
System.out.println(i);
}catch(Exception e) {
e.printStackTrace();
}
}
}
Output:
9
例二
public class Test {
public static void main(String[] args) {
int i;
try {
i=9;
}catch(Exception e) {
e.printStackTrace();
}
System.out.println(i);//编译时出错
//The local variable i may not have been initialized
}
}
其实i=9的确会在运行时赋值给 int i,并最终使i的值变成9,但是,编译期不允许。
例三
public class Test {
public static void main(String[] args) {
int i = 11;
try {
i=9;
}catch(Exception e) {
e.printStackTrace();
}
System.out.println(i);
}
}
Output:
9