zoukankan      html  css  js  c++  java
  • try-catch-finally 与返回值的修改

    先看一段java代码,func返回值为int:

     1 public static int func() {
     2     int result = 0;
     3     try {
     4         result = 1;
     5         return result;
     6     } catch (Exception e) {
     7         result = 2;
     8         return result;
     9     } finally {
    10         result = 3;
    11     }
    12 }

    正确的返回结果是,func返回1。


    分析可总结为:

      如果finally中没有return语句,但是改变了要返回的值,这里有点类似与引用传递和值传递的区别,分以下两种情况,:

      1)如果return的数据是基本数据类型或文本字符串**,则在finally中对该基本数据的改变不起作用,try中的return语句依然会返回进入finally块之前保留的值。

      2)如果return的数据是引用数据类型,而在finally中对该引用数据类型的属性值的改变起作用,try中的return语句返回的就是在finally中改变后的该属性的值。


    return 是String类型的code:

     1 public static String funcStr() {
     2         String result = "hello";
     3         try {
     4             result = "1";
     5             return result;
     6         } catch (Exception e) {
     7             result = "2";
     8             return result;
     9         } finally {
    10             result = "3";
    11         }
    12     }

    该函数返回值为字符串”1”。


    return 是引用数据类型(对象):

     1 public static Person funcPerson() {
     2         Person result = new Person(20);
     3         try {
     4             result.age = 30;
     5             return result;
     6         } catch (Exception e) {
     7             result.age = 40;
     8             return result;
     9         } finally {
    10             result.age = 50;
    11         }
    12     }
    13 
    14     static class Person {
    15         public int age;
    16         public Person(int age) {
    17             this.age = age;
    18         }
    19     }

    该函数的返回类型是resultPerson,age为50,即在finally中更改了有效。


    总结:

      如果没有异常出现,而且finally语句中没有return,则会执行try里边的return,并且,会将变量暂存起来(对象存的是引用的地址),再去执行finally中的语句,这时候,如果返回值是基本数据类型或者字符串,则finally相当于更改副本,不会对暂存值有影响;但是,如果返回值是对象,则finally中的语句,仍会根据地址的副本,改变原对象的值。所以上边的例子,返回值的age为50。

  • 相关阅读:
    POJ 3660 Cow Contest (floyd求联通关系)
    POJ 3660 Cow Contest (最短路dijkstra)
    POJ 1860 Currency Exchange (bellman-ford判负环)
    POJ 3268 Silver Cow Party (最短路dijkstra)
    POJ 1679 The Unique MST (最小生成树)
    POJ 3026 Borg Maze (最小生成树)
    HDU 4891 The Great Pan (模拟)
    HDU 4950 Monster (水题)
    URAL 2040 Palindromes and Super Abilities 2 (回文自动机)
    URAL 2037 Richness of binary words (回文子串,找规律)
  • 原文地址:https://www.cnblogs.com/sunweiye/p/10952319.html
Copyright © 2011-2022 走看看