Does use of final keyword in Java improve the performance?

前端 未结 13 1084
粉色の甜心
粉色の甜心 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:18

    final keyword can be used in five ways in Java.

    1. A class is final
    2. A reference variable is final
    3. A local variable is final
    4. A method is final

    A class is final: a class is final means we cannot be extended or inheritance means inheritance is not possible.

    Similarly - A object is final: some time we does not modified the internal state of object so in such case we can specify the object is final object.object final means not variable also final.

    Once reference variable is made final, it cannot be reassigned to other object. But can change the contents of the object as long as its fields are not final

提交回复
热议问题