Assigning “null” to objects in every application after their use

前端 未结 14 788
感动是毒
感动是毒 2021-01-18 12:05
  • Do you always assign null to an object after its scope has been reached?

  • Or do you rely on the JVM for garbage collection?

14条回答
  •  清歌不尽
    2021-01-18 12:32

    I assume you're asking this question because you've seen code with variables being assigned to null at the point where they will never be accessed again.

    I dislike this style, but another programmer used it extensively, and said he was taught to do so at a programming course at his university. The reasoning he gave is that it would prevent undetectable bugs if he tried to reuse the variable later on, instead of indeterminate behavior, he'd get a null pointer exception.

    So if you're prone to using variables where you shouldn't be using variables, it might make your code more easy to debug.

提交回复
热议问题