A Return inside and outside an If Statement

前端 未结 2 758
忘了有多久
忘了有多久 2021-02-10 01:56

This is probably a fairly easy question to answer, but it has been bugging me some time.

If there is a return statement inside an if statement, inside a method (in the J

相关标签:
2条回答
  • 2021-02-10 02:26

    No, both values aren't going to be returned. A return statement stops the execution of the method right there, and returns its value. In fact, if there is code after a return that the compiler knows it won't reach because of the return, it will complain.

    You don't need to use a variable outside the if to return it at the end. However, if your method is long and complex, this technique can help readability and clarity because only one return statement is used.

    0 讨论(0)
  • 2021-02-10 02:27

    Only the first return statement hit is used. The method then terminates.

    There are some code conventions that frown on multiple return statements because they might be hard to read, but I'm not one of them. :)

    0 讨论(0)
提交回复
热议问题