星空网 > 软件开发 > Java

数梦工厂笔试题回顾一

数梦工厂笔试题回顾一数梦工厂笔试题回顾一
 1 package test; 2  3 public class TestFinally { 4   public static void main(String[] args) { 5     Demo demo=new Demo(); 6     TestFinally tf=new TestFinally(); 7     String result=tf.fun1(demo); 8     System.out.println(result); 9     System.out.println(demo.str);10   }11   private String fun1(Demo demo) {12     try {13       demo.str+="1";14       return fun2(demo);15     }finally{16       demo.str+="2";17     }18   }19   private String fun2(Demo demo) {20     try {21       demo.str+="3";22       return demo.str;23     }finally{24       demo.str+="4";25     }26   }27 }28 class Demo{29   String str="";30 }

View Code

执行结果:

13

1342

我写的答案是result=1234 demo.str=1234。在我的记忆中finally是异常捕获关键词,finally代码块一定会执行。但我对其的执行过程并不十分清楚,以前一直认为一旦遇到return返回,即代表函数结束,则finally在return之前执行。所以写出了如上错误结果。而通过正确结果,可以看出,函数在return执行完毕后如果有finally代码块存在,就会在return执行完毕的基础上继续执行finally中的代码块。所以结果为result=12 demo.str=1324。

 

进一步的,那如果finally中也有return将怎么执行呢?

(1)

数梦工厂笔试题回顾一数梦工厂笔试题回顾一
 1 package test; 2  3 public class TestFinally { 4   public static void main(String[] args) { 5     Demo demo=new Demo(); 6     TestFinally tf=new TestFinally(); 7     String result=tf.fun1(demo); 8     System.out.println(result); 9     System.out.println(demo.str);10   }11 12   private String fun1(Demo demo) {13     try {14       demo.str+="1";15       return fun2(demo);16     }finally{17       18       demo.str+="2";19     }20   }21 22   private String fun2(Demo demo) {23     try {24       demo.str+="3";25       return demo.str;26     }finally{27       demo.str+="4";28       return demo.str;//添加的return语句29     }30   }31   32   33 }34 class Demo{35   String str="";36 }

View Code

执行结果:

134
1342

 

(2)

数梦工厂笔试题回顾一数梦工厂笔试题回顾一
 1 package test; 2  3 public class TestFinally { 4   public static void main(String[] args) { 5     Demo demo=new Demo(); 6     TestFinally tf=new TestFinally(); 7     String result=tf.fun1(demo); 8     System.out.println(result); 9     System.out.println(demo.str);10   }11 12   private String fun1(Demo demo) {13     try {14       demo.str+="1";15       return fun2(demo);16     }finally{  17       demo.str+="2";18       return demo.str;//添加的return语句19     }20   }21 22   private String fun2(Demo demo) {23     try {24       demo.str+="3";25       return demo.str;26     }finally{27       demo.str+="4";28     }29   }30   31   32 }33 class Demo{34   String str="";35 }

View Code

执行结果:

1342
1342

说明如果finally中也有return。则由于函数在return执行完毕后如果有finally代码块存在,就会在return执行完毕的基础上继续执行finally中的代码块,则finally中的return会覆盖try代码块中的return的结果。




原标题:数梦工厂笔试题回顾一

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

绝招!如何亚马逊避免长期仓储费?:https://www.ikjzd.com/articles/135261
最新A9算法量化极速爆款:https://www.ikjzd.com/articles/135262
“黑五”失声 跨境进口的明天会好吗?:https://www.ikjzd.com/articles/135263
关于亚马逊直播的一些技巧:https://www.ikjzd.com/articles/135264
欧洲KYC需要准备什么:https://www.ikjzd.com/articles/135265
黄五大促进行中:盘点NOON的绝杀营销:https://www.ikjzd.com/articles/135266
湘西游轮六 湘江游轮夜游:https://www.vstour.cn/a/411226.html
携程旅游网折扣优惠最新攻略(携程旅游网更佳折扣优惠):https://www.vstour.cn/a/411227.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流