Does use of final keyword in Java improve the performance?

前端 未结 13 1220
粉色の甜心
粉色の甜心 2020-11-22 08:42

In Java we see lots of places where the final keyword can be used but its use is uncommon.

For example:

String str = \"abc\";
System.ou         


        
13条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 09:08

    As mentioned elsewhere, 'final' for a local variable, and to a slightly lesser extent a member variable, is more a matter of style.

    'final' is a statement that you intend the variable to not change (i.e., the variable won't vary!). The compiler can then help you out by complaining if you violate your own constraint.

    I share the sentiment that Java would have been a better language if identifiers (I'm sorry, I just cannot call a non-varying thing a 'variable') were final by default, and required you to explicitly say that they were variables. But having said that, I don't generally use 'final' on local variables that are initialized and never assigned; it just seems too noisy.

    (I do use final on member variables)

提交回复
热议问题