高手帮我解决个笔试题

sl19841106123 2010-03-23 09:34:37

package test;

public class test {
static{
int x=5;
}
static int x,y;
public static void main(String[] args) {
x--;
myMethod();
System.out.println(x + y + ++x);

}

public static void myMethod(){
y = x++ + ++x;
}
}


结果是3 不知道为啥,高手给解释下
...全文
609 52 打赏 收藏 转发到动态 举报
写回复
用AI写文章
52 条回复
切换为时间正序
请发表友善的回复…
发表回复
moon&sean 2010-06-13
  • 打赏
  • 举报
回复
[Quote=引用 48 楼 warrior511 的回复:]
2楼正解,关键在于清楚
static {
int x= 5;
}
基本上可以理解为"无效",int x= 5的作用范围只在{}内,执行x--时,因为x没有初始化,默认为0,x--后x = -1;
y = x++ + ++x (-1 + 1,y = 0,x = 1)
执行MyMethod();
x + y ++x = 1+0+2=3
[/Quote]

支持
zliuzz 2010-04-19
  • 打赏
  • 举报
回复
我觉得...写成这样代码在公司 会被雷劈
penjian 2010-04-18
  • 打赏
  • 举报
回复
学习了
wangfanee 2010-04-18
  • 打赏
  • 举报
回复
学习了啊,没注意临时变量。。
warrior511 2010-04-18
  • 打赏
  • 举报
回复
2楼正解,关键在于清楚
static {
int x= 5;
}
基本上可以理解为"无效",int x= 5的作用范围只在{}内,执行x--时,因为x没有初始化,默认为0,x--后x = -1;
y = x++ + ++x (-1 + 1,y = 0,x = 1)
执行MyMethod();
x + y ++x = 1+0+2=3
groovy2007 2010-04-18
  • 打赏
  • 举报
回复
y = x++ + ++x;

这是谁出的题,直接把出题人拖出去打
huhongguang 2010-04-18
  • 打赏
  • 举报
回复
public class JdomXML {

static{
int x=5;
}

static int x,y;

public static void myMethod(){
System.out.println("myMethod before: x=" + x + ",y=" + y);
y = x++ + ++x;
System.out.println("myMethod after: x=" + x + ",y=" + y);
}

public static void main(String[] args) {
System.out.println("before: x=" + x);
x--;
System.out.println("after: x=" + x);

myMethod();

System.out.println("main before: x=" + x + ",y=" + y);
System.out.println(x + y + ++x);
System.out.println("main after: x=" + x + ",y=" + y);
}
}
mvcvip 2010-04-18
  • 打赏
  • 举报
回复
[Quote=引用 44 楼 lijianjian578286 的回复:]
引用 1 楼 wwwtyb 的回复:
这个题目的关键在于
static{
int x=5;
}
里面的变量x是个临时变量
所以test类成员x的值为0
x--; -- x值为-1

myMethod();
y = x++ + ++x; -- y=-1 + 1 = 0; x=1;

System.out.println(x + y + ++x); -- 1+0+2=3

……
[/Quote]
UP
jacky花园 2010-04-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wwwtyb 的回复:]
这个题目的关键在于
static{
int x=5;
}
里面的变量x是个临时变量
所以test类成员x的值为0
x--; -- x值为-1

myMethod();
y = x++ + ++x; -- y=-1 + 1 = 0; x=1;

System.out.println(x + y + ++x); -- 1+0+2=3
[/Quote]

up
afunx 2010-04-04
  • 打赏
  • 举报
回复
static{
int x=5;
}
叫初始化块。只有当类第一次加载时才会初始化。
package test;

public class test {
static{
int x=5;
}
static int x,y;
public static void main(String[] args) {
x--;//此时由于test类未被加载,即x未初始化,因为x类型为int,所以x本来是0,执行完后变为-1
myMethod();//test类依旧未被初始化,所以y=-1+1=0,x=1
System.out.println(x + y + ++x);//1+0+2=3

}

public static void myMethod(){
y = x++ + ++x;
}
}
麦穗 2010-04-04
  • 打赏
  • 举报
回复
1L正解
和稀泥 2010-04-01
  • 打赏
  • 举报
回复
支持下,好题
applerockhool 2010-04-01
  • 打赏
  • 举报
回复
杯具了,这算个什么题啊
himlys 2010-03-31
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 xbgordon 的回复:]

为啥默认的就是0呢
[/Quote]
这个得问sun
Gogo520520 2010-03-31
  • 打赏
  • 举报
回复
学习了
xiaohuli1984 2010-03-31
  • 打赏
  • 举报
回复
过来学习 最近面试中
ermoxin 2010-03-31
  • 打赏
  • 举报
回复
学习了,学习了。
lgy3622 2010-03-31
  • 打赏
  • 举报
回复
学习了,谢谢指点
liushengmz 2010-03-31
  • 打赏
  • 举报
回复
妈的。。。也受骗了。。。给

static{
int x = 5;
}

给这个int害了。。。。。如果没有这个的话

结果应该是23
岳丰源 2010-03-31
  • 打赏
  • 举报
回复
基础题目,考查的挺细的啊
加载更多回复(32)

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧