Does using final for variables in Java improve garbage collection?

后端 未结 15 2130
南笙
南笙 2020-11-29 18:01

Today my colleagues and me have a discussion about the usage of the final keyword in Java to improve the garbage collection.

For example, if you write a

15条回答
  •  粉色の甜心
    2020-11-29 18:37

    Well, I don't know about the use of the "final" modifier in this case, or its effect on the GC.

    But I can tell you this: your use of Boxed values rather than primitives (e.g., Double instead of double) will allocate those objects on the heap rather than the stack, and will produce unnecessary garbage that the GC will have to clean up.

    I only use boxed primitives when required by an existing API, or when I need nullable primatives.

提交回复
热议问题