关于变量初始化的问题!
Q:
What would be the result of attempting to compile and run the
following piece of code?
public class Test {
public int x;//注意哦,就是这一行,还有下面有注释的一行
public static void main(String args[]){
System.out.println("Value is " + x);
}
}
A. The output "Value is 0" is printed.
B. Non-static variable x cannot be referenced from a static context..
C. An "illegal array declaration syntax" compiler error occurs.
D. A "possible reference before assignment" compiler error occurs.
E. An object of type ArrayIndexOutOfBoundsException is thrown.
答案:B.
Q:
What would be the result of attempting to compile and run the
following piece of code?
public class Test {
public static void main(String args[]){
int x;//注意哦,就是这一行,还有上面有注释的一行
System.out.println("Value is " + x);
}
}
A. The output "Value is 0" is printed.
B. An object of type NullPointerException is thrown.
C. An "illegal array declaration syntax" compiler error occurs.
D. A "possible reference before assignment" compiler error occurs.
E. An object of type ArrayIndexOutOfBoundsException is thrown.
答案:D.
java中间有这样的定义吗?
即方法外面的变量会自动赋默认值,而方法里面的
变量必须初始化才能使用!