Do javac or Hotspot automatically add 'final' as an optimisation of invariant variables?

后端 未结 4 1997
北荒
北荒 2021-01-13 11:40

The consensus seems to be that there is a performance benefit to marking member variables as final because they never need reloading from main memory. My question is, do jav

4条回答
  •  感情败类
    2021-01-13 12:31

    Like many performance "enhancements" it is usually a better to ask; What is easier to understand and reason about? e.g. if a field is final I know it won't be changed anywhere. This is often leads to more optimial code, but more importantly it should be more maintainable code. ;)

    Certainly, I make any field which can be final as final. Personally I would have preferred that final be the default behaviour and you had to use a keyword like var to make it mutable.

提交回复
热议问题