public class Main {     public static void main(String[] args) { 
        System.out.println(test()); 
    }     public static boolean test() { 
        boolean b = false; 
        try {             return b = 4 > 3; 
        } finally { 
            System.out.println(b); 
        } 
    }  
}   那位能帮忙解释一下这段程序,我运行的结果是 两个 True; 不胜感激;

解决方案 »

  1.   

    public class Main {     public static void main(String[] args) { 
            System.out.println(test()); 
        }     public static boolean test() { 
            boolean b = false; 
            try {             return b = 4 > 3; 
            } finally { 
                System.out.println(b); 
            } 
        }  
    } 那位能帮忙解释一下这段程序的运行过程,我运行的结果是 两个 True; 不胜感激; 
      

  2.   

    答:1)当执行到: return b = 4 > 3; 时,return 不会立即返回,必须执行完全 finally {}部分,才能返回.
    因此:这时的b值是true2)执行: finally {}部分,打印出b值,输出true
    3)此时:return 返回b的值,即:true
    4)最后在main()中执行:System.out.println(...); 输出true
      

  3.   


    我也是这么觉得的。你可以在finally中改变输出字符,就知道结论了。
      

  4.   

    答:为给楼主加深理解,我有两个例子,请楼主思考:
    1)
    public class Main1 {     public static void main(String[] args) { 
            System.out.println(test()); 
        }     public static int test() { 
            int b = 1; 
            try {             return b ; 
            } finally { 
                b++; 
                
            } 
        }  

    则:程序的输出结果是 1 还是 2?2)若程序改为:public class Main1 {     public static void main(String[] args) { 
            System.out.println(test()); 
        }     public static int test() { 
            int b = 1; 
            try {             return b ; 
            } finally { 
                b++; 
                return b ;
            } 
        }  

    则:程序的输出结果是 1 还是 2?