public class Test
{
public static void main(String[] args)
{
System.out.print(tt());
}
public static int tt()
{
int b = 23;
try
{
System.out.println("yes");
return b += 88;
} catch (Exception e)
{
System.out.println("error : " + e);
} finally
{
if (b > 25)
{
System.out.println("b>25 : " + b);
}
System.out.println("finally");
}
System.out.println("come here");
return b;
}
}
运行结果:
yes
b>25 : 111
finally
111
这说明执行到return b += 88;这句时,先计算出了b=111,但不return,再跳到finally执行。
=====================================================================
public class Test1
{
public static void main(String[] args)
{
System.out.print(tt());
}
public static int tt()
{
int b = 23;
try
{
System.out.println("yes");
return b += 88;
} catch (Exception e)
{
System.out.println("error : " + e);
} finally
{
if (b > 25)
{
System.out.println("b>25 : " + b);
}
System.out.println("finally");
return 100;
}
}
}
运行结果:
yes
b>25 : 111
finally
100
return 100;覆盖掉了return b += 88;
====================================================================
public class Test2
{
public static void main(String[] args)
{
System.out.print(tt());
}
public static int tt()
{
int b = 23;
try
{
System.out.println("yes");
return b += 88;
} catch (Exception e)
{
System.out.println("error : " + e);
} finally
{
if (b > 25)
{
System.out.println("b>25 : " + b);
}
System.out.println("finally");
b = 100;
}
return b;
}
}
运行结果:
yes
b>25 : 111
finally
111
说明了一个问题,如果finally语句中没有返回语句覆盖的话,那么原来的返回值就不会变,不管你是不是改变了要返回的那个变量.