"In-depth understanding of the Java virtual machine" after reading - final

**

After a rough reading of "In-depth Understanding of the Java Virtual Machine", thinking on the final keyword

**

Recently, after reading "In-depth Understanding of the Java Virtual Machine", I have some thoughts, so I write this article to express my views. The author has studied "Java Programming Thoughts" a little last year, and has a certain impression of the ***higher performance*** expression of the final keyword in the book.

Before reading "In-depth Understanding of the Java Virtual Machine", the basic understanding of final is:
1. Final modified variables are immutable, and some variables are sublimated to constants;
2. Final modified methods cannot be rewritten;
3. final The location of the modified object in the heap is immutable;
4. The final and static modified static constants are "allocated on the stack" in the method area and are shared globally;
5. The final modified class cannot be inherited of;

After reading "In-depth Understanding of the Java Virtual Machine". Another understanding of final is that
1.final-modified variables are immutable. Whether in private working memory or main memory, final-modified variables do not have cache consistency issues; therefore, when the virtual machine is compiled, There is no need to verify final modified variables, which reduces the verification time;
2. For final modified methods, the virtual machine can directly use "method inlining" when compiling, reducing unnecessary method call costs, The establishment of stack frames is reduced, and the process of popping and pushing the stack of the method is reduced.

Although brute force reflection can modify constants, in most cases, constants are still safe.

The writing style is limited, so I only write for the afterthought.

Guess you like

Origin blog.csdn.net/qq_39414348/article/details/113181991