当前位置: 技术问答>java相关
关于变量初始化的问题!
来源: 互联网 发布时间:2015-03-01
本文导语: 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 "...
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中间有这样的定义吗?
即方法外面的变量会自动赋默认值,而方法里面的
变量必须初始化才能使用!
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中间有这样的定义吗?
即方法外面的变量会自动赋默认值,而方法里面的
变量必须初始化才能使用!
|
是的,的确有这样的定义的
|
楼上,第一个问题很清楚,对非Static的类成员必须用类的实例来调用
第二个问题你是不是抄错了,连初始化都没有,怎么调用变量x啊?
第二个问题你是不是抄错了,连初始化都没有,怎么调用变量x啊?
|
方法外面的变量是类的成员,即属性,会自动分配一个默认值(但不能使用),必须初始化以后才能访问.
方法内部的变量是局部变量
方法内部的变量是局部变量
|
刚才试了一下,class level的变量自动分配的默认值是可以直接使用的.
操,我看的书太旧了,Sorry
操,我看的书太旧了,Sorry