一段奇怪的Java,求解释~~~

云眸 2013-08-22 10:53:57

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
int a = Test.test();
System.out.println(a);
}

static int test() {
int x = 1;
try {
return x;
}
finally
{
++x;
}
}
}

以上代码片段为什么输出1呢?求解释~~~
...全文
660 18 打赏 收藏 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
ganshenml 2013-10-16
  • 打赏
  • 举报
回复
你把finally里面改成return ++x; 你会得到另外一个答案! 为什么呢? 因为finally语句是执行在return中间的,第一个try里面的return执行后并没有结束语句,之后进行了finally里面的语句,然后进行了第二次return语句,所有你就得到了另外一种答案!
guan_tu 2013-10-16
  • 打赏
  • 举报
回复
引用 2 楼 u011332678 的回复:
static int test(); Code: 0: iconst_1 1: istore_0 2: iload_0 3: istore_2 4: iinc 0, 1 7: iload_2 8: ireturn 9: astore_1 10: iinc 0, 1 13: aload_1 14: athrow 上面test方法所生成的字节码,可以看到虚拟机会先把要返回的结果用一个局部变量存起来,等执行完finally后再返回这个局部变量的值,所以在finally里面对x的改变不会对返回值造成影响。
+1
小行星0906 2013-10-16
  • 打赏
  • 举报
回复

public class Test
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		int a = Test.test();
		System.out.println(a);
	}

	static int test()
	{
		int x = 1;
		try
		{
			return x;
			// System.out.println("是先return了还是先finally");
		}
		finally
		{
			System.out.println("finally干活没,++x前:" + x);
			++x;
			System.out.println("++x后:" + x);
		}
	}
}
//
// 这题真坑爹,被谁翻上来了,下面是我能想到知识点:
//
// 1、++x和x++区别
//
// 2、try...finally执行
//
// 3、finally和return
//
// 4、和static{} 静态代码段区分(看的眼花,我困了估计)
//
// 5、JVM原理
每天三道题 2013-10-16
  • 打赏
  • 举报
回复
x的值在finally之前就返回了,finally中的++x不会影响返回值
liujun3512159 2013-08-24
  • 打赏
  • 举报
回复
帮忙顶一下。
qwerasd789 2013-08-24
  • 打赏
  • 举报
回复
已经return回去了,所以fially那段已经没有影响力
harrisonkao 2013-08-23
  • 打赏
  • 举报
回复
楼上是幼教吗
owen1759 2013-08-23
  • 打赏
  • 举报
回复
小明买苹果 小明手里有一个苹果 小红问小明:“你有几个苹果” 小明回答:“******” 小红走后,小明又买了个苹果 现在你问小红:“小明有几个苹果”,你猜小红会怎么说? 如果小红说“小明有两个苹果”,那就见了鬼了
owen1759 2013-08-23
  • 打赏
  • 举报
回复
引用 5 楼 swift12345 的回复:
...不是1是几,我还奇怪了
+1
安特矮油 2013-08-22
  • 打赏
  • 举报
回复
应该是函数已经得到返回值了,准备返回,但是由于有finally,所以需要去执行finally中的代码。如果finally能影响函数的返回值,那么aop不是能改变整个程序的结果了?这没有安全性可言了。
xy_jj 2013-08-22
  • 打赏
  • 举报
回复
在try里面已经return x;所以a=x=1;但是finally还会执行,所以x=2但是已经没有变量来接受它了,如果你想看到它在Test里定义一个类变量,代码如下: public class Test { /** * @param args */ static int x; public static void main(String[] args) { int a = Test.test(); int b = Test.x; System.out.println(a); System.out.println(b); } static int test() { try { x = 1; return x; } finally { ++x; } } } 求给分。
livend 2013-08-22
  • 打赏
  • 举报
回复
...不是1是几,我还奇怪了
dwwhu 2013-08-22
  • 打赏
  • 举报
回复
在执行++x之前x的值已经被返回了
_jerrytiger 2013-08-22
  • 打赏
  • 举报
回复
引用 1 楼 lcf 的回复:
    static int test() {
        int x = 1;
        try {
            return x;
        }
        finally
        {
            ++x;
        }
    }
约等于:
    static int test() {
        int x = 1;
        int temp = x;
       ++x;
        return temp;
    }
大牛 你好
dreamlee4711 2013-08-22
  • 打赏
  • 举报
回复
static int test(); Code: 0: iconst_1 1: istore_0 2: iload_0 3: istore_2 4: iinc 0, 1 7: iload_2 8: ireturn 9: astore_1 10: iinc 0, 1 13: aload_1 14: athrow 上面test方法所生成的字节码,可以看到虚拟机会先把要返回的结果用一个局部变量存起来,等执行完finally后再返回这个局部变量的值,所以在finally里面对x的改变不会对返回值造成影响。
lcf 2013-08-22
  • 打赏
  • 举报
回复
    static int test() {
        int x = 1;
        try {
            return x;
        }
        finally
        {
            ++x;
        }
    }
约等于:
    static int test() {
        int x = 1;
        int temp = x;
       ++x;
        return temp;
    }
825609451 2013-08-22
  • 打赏
  • 举报
回复
当执行的return时 x已经进入一个类似缓存的等待中 然后执行finally,x++,把缓存的x返回,所以返回x不等于2

62,620

社区成员

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

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