你只在try中返回,并没有在finally代码块中返回,所以返回的当然是1啦!将你代码稍该了一下输出就是2了。- package cn.liangfan.learningProblems;
- public class TestDemo2 {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- System.out.println(new TestDemo2().test());;
- }
- public static int test()
- {
- int x = 1;
- try
- {
- //return x;
- }
- finally
- {
- return ++x;
- }
- }
- }
复制代码
|