Understanding the 'finally' block

后端 未结 6 1009
误落风尘
误落风尘 2021-01-13 04:23

I\'ve written seven test cases for understanding the behavior of the finally block. What is the logic behind how finally works?

pac         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-13 04:30

    builder = null and builder.append("+1") are working. It's just that they're not affecting what you're returning. The function returns what the return statement has, regardless of what happens afterward.

    The reason there is a difference is because builder is passed by reference. builder=null changes the local copy of builder. builder.append("+1") affects the copy held by the parent.

提交回复
热议问题