Closed Alice52 closed 3 years ago
基本类型在 finally 中修改, 且之前已经返回, 则 finally 中的修改是无效的.
引用类型在 finally 中修改, 且之前已经返回, 则 finally 中的修改是有效的.
finally 保护问题:
sample code
@Test public void testTry() { assert changeBasicInFinally() == 1; assert changeReferenceInFinally().name().equals("tim"); assert returnBasicInFinally() == 2; } public int changeBasicInFinally() { int i; try { i = 1; return i; } finally { i = 2; } } public int returnBasicInFinally() { int i; try { i = 1; return i; } finally { i = 2; return i; } } public Person changeReferenceInFinally() { Person person = Person.builder().age(18).name("zack").build(); try { return person; } finally { person.name("tim"); } }
finally 中修改值得问题
基本类型在 finally 中修改, 且之前已经返回, 则 finally 中的修改是无效的.
引用类型在 finally 中修改, 且之前已经返回, 则 finally 中的修改是有效的.
finally 保护问题:
sample code