In Java, the final keyword can be used to decorate classes, methods, and variables (including member variables and local variables).
- When a class is decorated with final, it indicates that the class cannot be inherited.
- For a final variable, if it is a variable of the base data type, its value cannot be changed once initialized, and if it is a variable of a reference type, it cannot be pointed to another object after it has been initialized.
- The final decorated member variable must be initialized at the time of definition or in the constructor, and once the assignment is not changed.
- When a variable is defined as a final type, it is used as a constant when it knows its exact value at compile time.
Final String a = "Hello"= "Hello"= "HelloWorld"= A + "world"= B + "World"= === e);
Output Result:
True
False
5. The final modified object content is variable, but the object is immutable. Such as:
Final MyClass MyClass = new MyClass ();
MYCLASS.I = myclass.i + 3;
System.out.println (++MYCLASS.I);
Output Result:
4
However, if you re-assign a value to MyClass, the compilation error will occur.
MyClass = new MyClass ();
6. For the method parameter is the final type
Public void teststring (final String str) { + = "333"; } // compilation produces an error and cannot be re-assigned Public void Teststringbuilder (final StringBuilder str) { str.append ("333");} // can be successfully compiled and executed, and output 333
Sum up. I don't think final can change anything but references, not references. When the reference address of the final modified variable is not changed, it is only possible to change the value.
The final parsing of Java