Final keyword analysis

final is a reserved keyword in Java, you can declare a variable , method , class .

What is the final variable / class / method?

Before any final variable is modified is the final variable, the final pre-defined class is modified final class, before any method is the final method is final modification.

A, final class

 

When modifying a class with a final, indicating that this class can not be inherited. The arrows point to the place, the phrase translates to English Cat class can not be inherited.

If the final keyword removed it. So as not being given!

Use final class to pay attention, once set final class can not be inherited 

 Two, final method

The following excerpt "Java programming ideas", Fourth Edition, page 143 :

"There are two reasons for using final approach first reason is that the locking method to prevent any inheriting class to modify its meaning; second reason is efficiency in an early version of Java implementation, the method will be final turn. embedded call. However, if the method is too large, you may not see any performance improvements brought about calls embedded in the recent versions of Java do not need to use these methods to optimize the final. "

This means that if the method is sufficiently complete, the subclass can be used without modifying the final method is modified to avoid subclass. final method is statically bound method which class would be determined at compile time it is good, so the final method is faster than the number of non-final method

Three, final variable

Is the final variable is modified is the final variable, then the question is, final variables and common variables in the end what's the difference?

 For chestnut:

public class Main {
   public static void main(String[] args) {
       String a = "xiaomeng2";
       final String b = "xiaomeng";
       String d = "xiaomeng";
       String c = b + 2;
       String e = d + 2;
       System.out.println((a == c));
       System.out.println((a == e));
   }
}

true

false

  • 变量a指的是字符串常量池中的 xiaomeng2;

  • 变量 b 是 final 修饰的,变量  b 的值在编译时候就已经确定了它的值,换句话说就是提前知道了变量 b 的内容到底是个啥,相当于一个编译期常量;

  • B + 2 variable c is obtained, since b is a constant, so a direct equivalent to the original value of a b (Xiaomeng) when used to calculate b, c so generated is also a constant, a is a constant, c is constants are xiaomeng2 while the Java constant pool xiaomeng2 only generate a single string, a and c are equal!

  • d is a pointer to xiaomeng constant pool, but because d is not final modification, that is to say in the use of d does not know in advance what the value of d is calculated so when e is not the same, then e is due to the use of d is calculated by reference, it takes a variable d to access at run time links, so this calculation will be generated on the heap xiaomeng2, so the final points to the heap e xiaomeng2, so a and e are not equal.

That is a and c are constant pool xiaomeng2, e is the heap xiaomeng2 

 

final modification and modifying variables reference variables What is the difference

Objects final modified constant variables can not be changed, modified reference variable references can not be changed, reference may change

For chestnut:

  

 Modify ordinary variables int error

Modified reference variable references can not be changed what it means

As another chestnut:

 test is a reference variable, the final modification, if then this test will be given a reference point to somewhere else

Object references may change and what it means 

For chestnut:

2 code execution result is not given, the object described test which increases the value of i is 1, the object becomes a reference in

final summary of key benefits:

  • final method is faster than some of the non-final

  • final keyword improved performance. JVM and Java applications are cached final variable.

  • final variables can safely be shared in a multithreaded environment, without the need for additional synchronization overhead.

  • The final keyword, JVM will method, class variables and optimization.

Guess you like

Origin www.cnblogs.com/zouwangblog/p/10984968.html