FINAL meaning
FINAL is a retaining keyword in Java, which can declare member variables, methods and classes. Once you will quote the statement as the final type, you will not change this reference again. The compiler will check the code. If you try to initialize the variable again, the compiler will report the compilation error.
final variable
Anyone variable or local variable (in the method or code block is called a local variable) declaration for membership variables or local variables. Final variables are called final variables. The following is an example of the Final modifier variable:
FINAL INTVALUE = 1; // ConstValue = 2; The Final Local Variable ConneNot Be AssiGNed.
Supplement point:
final method
FINAL can also declare the method. The method name is added with the FINAL keyword, which means that this method cannot be rewritten by subclasses. If you think that the function of a method is complete and the method does not need to change the method in the subclass, you can declare the method as the final type.
The method declared by Final is faster than the non -final method, because it has been static binding when compiling, and it does not need to be dynamically binding at runtime. The following is an example of the final method:
public class person {public final void printname () {system.out.println ("wangzhengyi");}}
final class
The class modified class is called the final class. The Final class is usually complete and they cannot be inherited. Many classes in Java are FINAL, such as String classes.
One of the benefits of unavailable categories is that it itself is a thread security. You don't need to consider thread security issues in a multi -threaded environment. To create unavailable categories, the following steps need to be achieved:
Note: The difference between shallow copy and deep copy:
Shallow copy (shallow cloning): All variables of the replicated object contain the same value as the original object, and the references of all other objects still point to the original object. In other words, only the basic types of objects are copied, and the type of objects still belongs to the original quotation.
Deep copy (deep clone): All copied variables contain the same value as the original object, except for variables that quote other objects. Variables that quote other objects will point to new objects that have been copied, and no longer the original objects referenced. In other words, not only the basic types of objects, but also the objects in the original object.
Unsmorable examples:
Import Java.util.hashmap; Import Java.util.itrator; Public Class FinalClassexample {Private Final INT ID; Private Final String name; Private Final Ha shmap <integer, string> hmap; public int getid () {return id;} public string GetName () {Return name;} / *** Variable objects: Copy of the return object* @Return* / public hashmap <integer, string> Gethmap () {Rashmap <Integer, ST. raing>) Hmap. clone ();} / *** The constructor of deep copying* / Public Finalclassexample (int ID, string name, hashmap <integer, string> map) {this.id = id; this.name = name; HashMap <Integer , String> Tmap = New HashMap <Integer, String> (); Int key; Iterator <Integer> Iterator = MAP.KEYSET (). While (Iterator.hasnext ()) {key = iter Ator.next () ; Tmap.put (key, map.get (key));} this.hmap = tmap;} /*** A implementation of shallow copies* / /* PUBLIC FINALCLASSEXAMPLE (INT ID, String Name, HashMap <INTT eger, string > map) {this.id = id; this.Name = name; this.hmap = map;} */}Final keyword benefits
The advantages of using Final keywords are as follows: