给出以下代码,请给出结果.
class Two{
Byte x;
}
class PassO{
public static void main(String[] args){
PassO p=new PassO();
p.start();
}
void start(){
Two t=new Two();
System.out.print(t.x+””);
Two t2=fix(t);
System.out.print(t.x+” ” +t2.x);
}
Two fix(Two tt){
tt.x=42;
return tt;
}
}
![](https://img.haomeiwen.com/i13039554/71053fb27355ca9f.png)
默认值
![](https://img.haomeiwen.com/i13039554/c44bf38ca950472c.png)
这个题让我想起了之前做过的一个题,考察的是
类变量有默认值,可以不初始化;但局部变量必须要初始化
举个例子
int a = 10;
int b;
int c;
@Test
public void test2(){
if (a > 50) {
b = 9;
}
c = b + a;
System.out.println(c); //10
}
@Test
public void test2(){
int a = 10;
int b; //Error: java: 可能尚未初始化变量b
int c;
if (a > 50) {
b = 9;
}
c = b + a;
System.out.println(c);
}
网友评论