final keyword analysis in Java

First, the basic use of the final keyword

1, modified class

   When modifying a class with a final, indicating that this class can not be inherited. Note: The final class member variables can be set to all members of the methods final, final classes needed will be implicitly designated as the final method .

 

2, modification methods

     The following excerpt "Java programming ideas" fourth edition, page 143: "There are two reasons for using a final method first reason is that the locking method to prevent any inheriting class to modify its meaning; the second reason. is efficiency. in earlier versions of Java implementation, will be built into final method call. but if the method is too large, you may not see any performance embedded calls to bring in the most recent version of Java, no use these methods to optimize the final. "

  The method explicitly prohibited only want a method is provided in a subclass of the case is covered by the final , i.e. final parent class is not covered by subclasses .

       This method represents a modification of the final method has been the "last, final" meaning, i.e., this method can not be rewritten (s final modification method can be overridden) .

      (Note: private class methods implicitly designated as the final method.)

3, modified variables 

      final member variable represents a constant, can only be assigned once, after the value assigned does not change. When a final modification of the basic data types, a value indicating the type of data base changes can not occur soon after the initialization;

      If the final modification of a reference type, then it can not let its initialization after point to other objects, but the contents of the reference points to an object that changes can occur.

       Essentially the same thing, since the reference value is an address, final required value, i.e. the value of the address does not change.

  final modification of a member variable (property) must be displayed initialized. There are two ways to initialize a variable is initialized when declared; The second method is not the initial value at the time you declare a variable, but to assign to this variable in all of this variable constructor where the class the initial value.

  When the function is declared parameter type final, indicating that the parameters are read-only type. Ie you can use this parameter to read, but can not change the value of the parameter.

 

Second, in-depth understanding of the final keyword

1, the final class variables and common variables What is the difference?

     When applied to a final class member variables, member variables (note that the class member variables, local variables only need to be assigned to the initialized before use ) must be defined or initialized at the time of the assignment in the constructor, and the final variable Once the assignment is initialized, it can not be assigned up.

     So what difference does it make final variables and common variables in the end? See the following example a:

Copy the code
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)); 
    } 
} 
Copy the code

Output: true, false
        , you can first think about this question of output. Why the first comparison is true, and the second comparison is fasle. There is the difference between final variables and variables of the ordinary , when the final variable is the basic data types and String type, if we can know its exact value during compilation, the compiler will use it as a compile-time constants. That is used in place of the final variable, the equivalent of direct access to the constant need not be determined at run time.

  Note, however, only during compilation can know exactly the case of final variable values, the compiler will perform this optimization, such as the following code will not be optimized:

Copy the code
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"; 
    } 
} 
Copy the code

The output of this code is to false . Point to note here is this: Do not think that some of the data is final can know the value at compile time by the variable b we know, here is the use of getHello () method to initialize it, he wants to know in order to run its value .

And examples of the above are not the same, do not need to know the value at runtime.

 

2, the final point of the modified reference variable content of the object variable you?

    After the final variable is modified, although it can not point to other objects, but the content it points to the object is variable .

3, issue final arguments

     In practical applications, we can use in addition to the modified final member variables, member methods, classes, parameters can also be modified, if a parameter is modified the final, it means the parameter can not be changed.

     

 

Guess you like

Origin www.cnblogs.com/651434092qq/p/11492619.html