This article mainly introduces the final keyword. Through the basic usage of the final keyword and the understanding of the final keyword, you can refer
This article mainly introduces the final keyword. Through the basic usage of the final keyword and the understanding of the final keyword, you can refer
When talking about final keywords, many people may be familiar with them, and they may often use final keywords when using anonymous internal classes. In addition, the String class in Java is a final class, so today we will learn about the usage of the final keyword.
I. Basic usage of final keywords. 2. in-depth understanding of final keywords
I. Basic usage of final keywords
In Java, final keywords can be used to modify classes, methods, and variables (including member variables and local variables ). The following describes the basic usage of the final keyword from these three aspects.
1. modifier class
When a class is modified with final, it indicates that the class cannot be inherited. That is to say, if you never let a class be inherited, you can use final to modify it. The member variables in the final class can be set as final as needed, but note that all member methods in the final class are implicitly specified as final methods.
When using final to modify a class, be careful when selecting it. Unless the class is not inherited in the future or for security reasons, try not to design the class as a final class.
2. Modification Method
The following passage is taken from page 143rd of the fourth edition of Java programming ideology:
"There are two reasons for using the final method. The first reason is to lock the method to prevent any inheritance class from modifying its meaning. The second reason is efficiency. In earlier Java implementations, the final method is converted into an embedded call. However, if the method is too large, you may not be able to see any performance improvement caused by embedded calls. In the latest Java version, the final method is not required for these optimizations. "
Therefore, if you want to explicitly prohibit this method from being overwritten in the subclass, set the method to final.
Note: The private method of the class is implicitly specified as the final method.
3. Modify Variables
Modifying variables is the most widely used part of final, and is also the focus of this article. First, let's take a look at the basic syntax of final variables:
For a final variable, if it is a basic data type variable, its value cannot be changed once it is initialized; if it is a reference type variable, after initialization, you cannot point it to another object.
For example:
In the above Code, an error is reported for the re-assignment of the variables I and obj.
2. in-depth understanding of final keywords
After learning about the basic usage of the final keyword, let's take a look at the obfuscation of the final keyword.
1. What is the difference between the final variable of the class and the common variable?
When final is used to act on the member variables of the class, the member variables (note that it is the member variables of the class, and the local variables only need to be initialized and assigned values before use) the value must be initialized at the time of definition or in the constructor. Once the final variable is initialized and assigned a value, it cannot be assigned again.
So what is the difference between final variables and common variables? The following is an example:
Public class Test {public static void main (String [] args) {String a = "hello2"; final String B = "hello"; String d = "hello "; string c = B + 2; String e = d + 2; System. out. println (a = c); System. out. println (a = e ));}}
True
False
You can first think about the output result of this question. Why is the first comparison result true, and the second comparison result fasle.
The difference between final variables and common variables is that when final variables are of the basic data type and String type, if the exact value of final variables can be known during compilation, the compiler uses it as a constant during the compilation period. That is to say, where the final variable is used, it is equivalent to directly accessing this constant and does not need to be determined at runtime. This is a bit like macro replacement in C.
Therefore, in the above Code, because variable B is modified by final, it will be treated as a compiler constant, so where B is used, it will directly replace variable B with its value. The access to variable d needs to be done through the link at runtime.
We should understand the differences, but note that the compiler will perform such optimization only when the final variable value is accurately known during compilation, for example, the following code won't be optimized:
Public class Test {public static void main (String [] args) {String a = "hello2"; final String B = getHello (); String c = B + 2; System. out. println (a = c);} public static String getHello () {return "hello ";}}
The output result of this Code is false.
2. is the content of the object pointed to by the reference variable modified by final variable?
As mentioned above, reference variables modified by final cannot point to other objects once they are initialized and assigned values. is the content of the object pointed to by the referenced variable? Let's look at the example below:
Public class Test {public static void main (String [] args) {final MyClass myClass = new MyClass (); System. out. println (++ myClass. i) ;}} class MyClass {public int I = 0 ;}
This code can be compiled smoothly and has output results. The output result is 1. This indicates that after the referenced variable is modified by final, although it cannot point to another object, the content of the object it points to is variable.
3. final and static
In many cases, it is easy to confuse static and final keywords. static acts on member variables to indicate that only one copy is saved, while final is used to ensure variable immutability. Let's look at the example below:
Public class Test {public static void main (String [] args) {MyClass myClass1 = new MyClass (); MyClass myClass2 = new MyClass (); System. out. println (myClass1. I); System. out. println (myClass1.j); System. out. println (myClass2. I); System. out. println (myClass2.j);} class MyClass {public final double I = Math. random (); public static double j = Math. random ();}
Run this code and you will find that the two j Values printed each time are the same, while the I values are different. Here we can know the difference between final and static variables.
4. Why can only external local variables used in anonymous internal classes be final variables?
For this question, please refer to the explanation in "Java internal class explanation" in the previous blog, and I will not go into details here.
5. Questions about final Parameters
"When you do not need to change the object variable as a parameter in the method, use final to declare it, it will prevent you from accidentally modifying the variables outside the call method. "I personally understand that this is inappropriate.
Because no matter whether the parameter is a variable of the basic data type or a variable of the reference type, using final declaration will not achieve the effect described above.
Let's see this example: