Http://www.cnblogs.com/xrq730/p/4820296.html
Always wanted to write this topic. On behalf of the company also interviewed a number of job seekers, one of the two questions I must ask in each interview is "Please talk about the final keyword understanding." This is a simple little problem, but don't underestimate it, and by answering the question and some simple extensions you can see whether a job seeker is based on a solid foundation or a spirit of research. OK, start entering the topic.
Believe that for final use, most people can say three words casually:
1. A class that is final modified cannot be inherited
2, the final modified method can not be rewritten
3. The final modified variable can not be changed
focus is the third sentence. 。 The final modified variable can not be changed, what can not be changed, is a reference to the variable? or the contents of a variable? Or are they both not to be changed? Write an example to see it:
public class finalstring{ String str; public Finalstring (String str) { this . Str = str; public String Getstr () { return str; public void Setstr (String str) { this . Str = str; }}
Public class test{ publicstaticvoid main (string[] args) { final New Finalstring ("1"); Fs.setstr ("2"); System.out.println (Fs.getstr ());} }
Run a little bit, no problem at all. Modify it a little bit:
Public Static void Main (string[] args) { finalnew finalstring ("1"); Final New Finalstring ("333"); = FSS;}
7th line Error, "The final local variable FS cannot be assigned". It can be seen that the final modification is immutable as a reference to a variable, not a reference to the content that the reference points to . OK, what about the final modified array?
Public Static void Main (string[] args) { final string[] Strs0 = {"123", "234"}; Final String[] strs1 = {"345", "456"}; = Strs0; strs1[1] = "333";}
Similarly, the 5th line error "The final local variable strs1 cannot be assigned", line 6th a little problem is not. Variables and arrays are referred to as immutable, and references to content are mutable . In fact, if you have used the FindBugs plug-in should know, if the code in the final decoration of an array, then the row code will be as a bug in the findbugs is found, because "the final modification of the array is meaningless."
Next, take a look at the scenario with the final Retouch method parameter:
public class test{ public static void Main (string[] args) {finalstring fs = new Finalstring (" private static void A (final finalstring FS) {FS.SETSTR ( "123" ); finalstring FSS = new finalstring (" = FSS; }}
Same, the same is 13 line error, 11 Line no problem, I believe you already know the reason.
Summarize
"Reference" is a very important concept in Java, it is not very understanding of reference, it is easy to make some mistakes that you are unaware of. The final modified variable, regardless of the variable is the variable, remember that immutable is a reference to the variable instead of referring to the content of the object . In addition, there are two points in this article about the final function that are not mentioned:
1. The final modified method, the JVM will try to seek the inline, which is very important to improve the efficiency of Java. Therefore, if you can determine that the method will not be inherited, then try to define the method as final, as described in the method inline section of the run-time optimization technique
2. The final modified constants are stored in the constant pool of the calling class during the compilation phase.
Understanding of the Final keyword