Public class finaltest {Public String findword = "final word"; public final stringbuffer sfindword = new stringbuffer ("final word "); /*** @ Param ARGs */public static void main (string [] ARGs) {finaltest Ft = new finaltest (); system. out. println (FT. findword. hashcode (); ft. findword + = "DD"; system. out. println (FT. findword. hashcode (); system. out. println (FT. sfindword. hashcode (); ft. sfindword. append ("DD"); system. out. println (FT. sfindword. hashcode ());}}
The running result is:
-1919389836
-1992659212
14576877
14576877
The final modifier points to a fixed address and cannot represent a fixed value.
It can be seen that final is only valid for the referenced "value" (that is, the memory address of the object it points to), and it forces the reference to only point to the object initially pointed, changing the point will cause compilation errors. Final is not responsible for the changes to the objects it points. This is similar to the = Operator: = Operator is only responsible for the equal value of the referenced "value". As to whether the content of the object pointed to by this address is equal, the = operator does not matter. Understanding final issues has important meanings. ManyProgramAll vulnerabilities are based on this ---- final can only ensure that the reference always points to a fixed object, but cannot ensure that the state of that object remains unchanged. In multi-threaded operations, an object will be shared or modified by multiple threads. Unintentional modifications made by one thread to the object may cause another thread to crash. The solution to an error is to declare the object as final when it is created, so that it is "never changed ". In fact, it is futile.