The final modifier variable cannot be changed, or the referenced object cannot be changed.
Check the Code:
1 package pnunu. oa. test; 2 3 public class TestFinal {4 public static void main (String [] args) {5 final StringBuilder sb = new StringBuilder ("123 "); 6 // The hashCode value of the same object is the same 7 System. out. println ("the content in sb is:" + sb); 8 System. out. the hashing of println (sb + "is:" + sb. hashCode (); 9 sb. append ("I changed"); 10 System. out. println ("the content in sb is:" + sb); 11 System. out. the hashing of println (sb + "is:" + sb. hashCode (); 12 System. out. println ("----------------- hash value -----------------"); 13 TestFinal test = new TestFinal (); 14 System. out. println (test. hashCode (); 15 System. out. println (Integer. toHexString (test. hashCode (); 16 System. out. println (test. getClass (). getName () + "@" 17 + Integer. toHexString (test. hashCode (); 18 // toString () defined in the API is equivalent to getClass (). getName () + '@' + 19 // Integer. toHexString (hashCode () 20 // the returned value is a string representation of the object.21 System. out. println (test. toString (); 22} 23}
Result:
The content in 1 sb is: 1232 123 the hash code is: 145768773 the content in sb is: 123 I changed 4 123 I changed the hash code is: 145768775 ----------------- hash value ------------------- 6 126774767 c171648 pnunu. oa. test. testFinal @ c171649 pnunu. oa. test. testFinal @ c17164
According to the analysis results, the hashCode is the same twice, indicating that the referenced variable has not changed. The content of the object to which the referenced variable points can still be changed.
In general, for a final variable, if it is a variable of the basic data type, its value cannot be changed once it is initialized;
If it is a reference type variable, it cannot point to another object after initialization.
Bytes ------------------------------------------------------------------------------------------------------
For example, for the following statement:
Final StringBuffer a = new StringBuffer ("immutable ");
Run the following statement to report compilation errors:
A = new StringBuffer ("");
However, the following statement can be compiled:
A. append ("broken! ");
When defining the parameters of a method, someone may want to use the following form to prevent the method from modifying the passed parameter object:
Public void method (final StringBuffer param ){}
In fact, this cannot be done. In this method, you can add the following code to modify the parameter object:
Param. append ("");
This is because the reference of the variable param has not changed, so there is no error.
However, the following error occurs, because int-type data is directly stored in the stack memory, and it does not need to be referenced like an object,
If x is modified, the reference is modified. Therefore, if x is modified, an error occurs.
Public int addOne (final int x ){
Return x ++;
}
Another example is as follows:
Public class Something {
Public static void main (String [] args ){
Other o = newOther ();
New Something (). addOne (o );
}
Public void addOne (final Other o ){
O. I ++;
}
}
Class Other {
Public int I;
}
This will not cause errors during compilation, because at this time, it is not the object reference but the object attribute, because only the object is of the final type, so as long as the object is not modified, no error will occur.