A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. public  class Test {
  2.         public static void main(String[] args) {
  3.         // TODO Auto-generated method stub
  4.         System.out.println(new Test().test());;
  5.         }
  6.             static int test()
  7.         {
  8.         int x = 1;
  9.         try
  10.         {
  11.            return x;
  12.         }
  13.         finally
  14.         {
  15.             ++x;
  16.         }
  17. }
复制代码
try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后?

评分

参与人数 1技术分 +1 收起 理由
赵志勇 + 1 赞一个!

查看全部评分

15 个回复

倒序浏览
会执行。return前
回复 使用道具 举报
闾丘日月 发表于 2012-6-6 08:21
会执行。return前

说说为什么啊
回复 使用道具 举报
先执行finally语句块;
不只return,当finally和break、continue一起使用的时候,程序必须先执行finally块,才能最终离开try语句块
回复 使用道具 举报
我记得好像是finally语句是无论如何都要被执行的,从运行结果上来看确实是这样的,并且是在return语句后面执行的,如果不相信你可以试试,
public  class Test {
        public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(new Test().test());;
        }
        static int test(){
                int x = 1;
                try{
                        System.out.println(x+" try");
                        return x;
                }finally{
            ++x;
            System.out.println(x+" finally");
                }
        }
}
回复 使用道具 举报
finally 是必需执行的 所以一些需要处理的都可以放在这里面
只有一种情况会阻止finally 子句执行, 就是虚拟机被关闭( 例如执行
System.exit方法,或是机器关闭,或是电源关闭)。这意味着控制流程可能脱
离正常的执行顺序。例如,如果有return在try语句块内的代码中,finally
子句中的代码会在返回前执行。

如果不用fianlly 不能保证那些资源得到处理
回复 使用道具 举报
会执行的,是在return前。
你可以在finally中加System.out.println("abc");一个打印语句试下,看会不会打印abc,
就知道了。
回复 使用道具 举报
只要try块开始执行了,finally块里面的代码保证执行一次并且只有一次
回复 使用道具 举报
本帖最后由 李天甲 于 2012-6-6 08:59 编辑

这个代码里面隐含了一点东西,就是为什么输出的值是1而不是2呢?
大家都觉得finally里面的东西都会执行,代码给了我们错觉,让我们觉得执行到x=1之后输出的值是1就不再执行++x了.
这是因为用的是int 类型的缘故,如果要是类型的话,返回的代码铁定是被finally修改之后的.呵呵..
上代码....
  1. class Test {
  2. public static void main(String[] args) {
  3. System.out.println(test().getName());
  4. }
  5. static Person test() {
  6. Person p=new Person("张三");
  7. try {
  8. System.out.println("之前输出");
  9. return p;
  10. } finally {
  11. p.setName("超级张三");
  12. System.out.println("执行了");
  13. }
  14. }
  15. }
复制代码
  1. class Person{
  2. String name;
  3. Person(String name) {
  4. this.name = name;
  5. }
  6. public String getName() {
  7. return name;
  8. }
  9. public void setName(String name) {
  10. this.name = name;
  11. }
  12. }
复制代码
插入代码怎么突然没格式了...

评分

参与人数 1技术分 +1 收起 理由
袁錦泰 + 1

查看全部评分

回复 使用道具 举报
李鑫 发表于 2012-6-6 08:22
说说为什么啊

要是先执行return,方法就结束了,就执行不到方法体内的finally了。
回复 使用道具 举报
  1. public  class Test1 {
  2.                 static int a = 0;
  3.         public static void main(String[] args) {
  4.                         a = test();
  5.                         System.out.println("a =" + a ); // 如果return ++x,这里是2,如果是return x++,这里是1。
  6.                
  7.         }
  8.         static int test()
  9.         {
  10.                         int x = 1;
  11.                         try
  12.                         {
  13.                                 return ++x;
  14.                                 // return x++;
  15.                         }
  16.                         finally
  17.                         {
  18.                                 System.out.println("x=" + x); // 这里打印结果是2,说明retrun中的x++运算已经执行。
  19.                                 System.out.println("a =" + a );//这里的a为0,说明retun并未直接将值返回给接收者,而是有一个延迟。
  20.                                
  21.                         }
  22.                        
  23.                 }
  24. }
复制代码
所以retun语句先执行,将值返回给了上级语句块— —  test()方法,当test()方法体中的所有语句全都执行完之后,由test()方法返回给调用者。try中的retun是有延迟的。

如果在finally语句块中也增加一条return语句,则try的返回值被覆盖,如下。一般情况return语句之后的代码都不会被执行,所有很少有覆盖的情况,由于finally一定会执行(JVM停止的情况除外),所以出现这种情况。
  1. public  class Test1 {
  2.                 static int a = 0;
  3.         public static void main(String[] args) {
  4.                         a = test();
  5.                         System.out.println("a =" + a ); // finnally 语句块中添加return后,之前try中的返回值被覆盖,这里结果为3.
  6.                
  7.         }
  8.         static int test()
  9.         {
  10.                         int x = 1;
  11.                         try
  12.                         {
  13.                                 return ++x;
  14.                                 // return x++;
  15.                         }
  16.                         finally
  17.                         {
  18.                                 System.out.println("x=" + x); // 这里打印结果是2,说明retrun中的x++运算已经执行。
  19.                                 System.out.println("a =" + a );//这里的a为0,说明retun并未直接将值返回给接收者,而是有一个延迟。
  20.                                 return ++x; //增加return 语句。
  21.                                
  22.                         }
  23.                        
  24.                 }
  25. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
袁錦泰 + 1

查看全部评分

回复 使用道具 举报
public static void main(String[] args) {
                System.out.println(f());
        }
        public static int f(){
                try{
                        return 1;
                }finally{
                        return 2;
                }
        }
输出结果是2,说明try里面return的值被finally里的return覆盖了。

评分

参与人数 1技术分 +1 收起 理由
袁錦泰 + 1

查看全部评分

回复 使用道具 举报
package luntantest;

public  class Test {
         
    public static void main(String[] args) {

    // TODO Auto-generated method stub

    System.out.println(new Test().test());;

    }

        static int test()

    {

    int x = 1;

    try

    {

       return x;
      

    }

    finally

    {
            System.out.println(x + "jjjj");

        ++x;
        System.out.println("this  is "+x);

       }

    }
  }
输出将结果为
1jjjj
this  is 2
1
这说明了会执行的并且是在之前执行的

评分

参与人数 1技术分 +1 收起 理由
袁錦泰 + 1

查看全部评分

回复 使用道具 举报
如果問題得到解決請修改題目為已解決...
回复 使用道具 举报
注意:return之前看有没有finally,有就需要执行

public class testReturn {
public static int test() {
try {
return fun1();
} catch (Exception e) {
} finally {
return fun2();
}
}

public static int fun1() {
System.out.println("fun1被执行了");
System.out.println("fun1的确被执行了,返回么?");
return 1;
}

public static int fun2() {
System.out.println("fun2被执行了");
System.out.println("fun2的确被执行了,返回么?");
return 2;
}

public static void main(String[] args) {
System.out.println(testReturn.test());
}
}
结果:fun1被执行了
fun1的确被执行了,返回么?
fun2被执行了
fun2的确被执行了,返回么?
2

//注意,结果中第二句之后并没有返回值(但此时x的值会被记录)
!程序之后暂时给finally操纵了,如果finally“把握住了机会”
用return返回了,那返回的是现在x的值,如果没把握住,不管它执
行了什么代码,返回的x值还是执行try那个时期记录的。

看完这你就明白了!!!
回复 使用道具 举报
那是我的笔记
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马