Detail the difference between final finalize finally in Java programming

  • 2020-04-01 04:24:29
  • OfStack

Final:
Final gives you control over whether your members, methods, or a class can be overridden or inherited. These features make final an indispensable part of Java and one of the key words to know and master when learning Java.
Members of the final
When you define a variable ina class, and you prefix it with the final keyword, that means that the variable is immutable once it's initialized, which means that the value is immutable for primitive types, and the reference is immutable for object variables. It can be initialized in one of two places, where it is defined and in the constructor, and you can only choose one from the other.
The following program simply demonstrates the general use of final:


public class Test{
 final int t = 1; //Give values at definition time
 //Or (one or the other)
 final int t;
 public Test(){
  t = 3; //Construct the value
}
}

Another use is to define the method of parameters for the final, the basic types of variables, do no practical significance, because the basic types of variables in the calling method is by value, that is to say, you can change the parameter variable in a method and will not affect the call statement, for object variables, however, seem to be very practical, because the object variable is passed its reference when passing, you amend in the method of the object variables will also affect the call object of the variables in the statement, when you do not need to change in the method as a parameter object variables, clear use their final statement, This will prevent you from inadvertently modifying the calling method.
In addition, when the inner class of the method USES the variables in the method, this parameter must be declared as final before it can be used, as shown in the following code:


public class Test{
 void print(final String str){
  class InnerTest{
   InnerTest (){
    System.out.println(str);
   }
  }
  InnerTest it=new InnerTest ();
 }
 public static void main(String[] args){
  Test test=new Test();
  test.print("Hello word!!!");
 }
}

The final method
There are two reasons for declaring a method final. The first is that you already know that the functionality provided by the method meets your requirements, you don't need to extend it, and you don't allow any classes inherited from this class to override the method, but inheritance can still inherit the method, which means you can use it directly. The second is to allow the compiler to convert all calls to this method to the inline (inline) call mechanism, it can make your final the call method, the method body directly inserted into the call, rather than routine method calls, such as saving breakpoints, pressure stack, etc., this may make your program efficiency is improved, but when your method when the subject is very large, or you call this method in many place, so you call the main body of code will rapidly expand, instead, may affect efficiency, so you have to careful final method definition.
Final class
When you use final on a class, you need to think carefully, because a final class cannot be inherited by anyone, which means that the class is a leaf in an inheritance tree, and that the design of the class is considered perfect without modification or extension. For members of a final class, you can define them as final or not final. Methods, however, are of type final because they belong to classes that are final. You can also explicitly add a final to a method ina final class, but that doesn't make sense.

Finally:
The finally keyword is the best complement to the Java exception handling model. A finally structure enables the code to always execute regardless of whether an exception occurs. Finally is used to maintain the internal state of an object and to clean up non-memory resources. Without finally, your code would be convoluted. For example, the following code shows how you can write code to free non-memory resources without using finally:


public void writeFile(String filePath, String fileName, String args)
   throws IOException

 {

  FileWriter fw = new FileWriter(filePath + fileName);
  try {

   fw.write(args);
  } catch (IOException e) {
   //1
   fw.close();
   throw e;
  }
//2
  fw.close();
 }

This code creates a FileWriter object and calls the write method. Before exiting the method, you must close the FileWriter object to avoid resource vulnerabilities. To do this, we call close at //2, which is the last statement of the method. But what happens if an exception occurs in the try block? In this case, the close call at //2 will never happen. Therefore, you must catch the exception and insert another call to close at //1 before issuing the exception again. This ensures that the FileWriter object is closed before exiting the method. Writing code this way is cumbersome and error-prone, but it is essential in the absence of finally. With finally, the previous code can be rewritten as follows:


public void writeFile(String filePath, String fileName, String args)
   throws IOException

 {

  FileWriter fw = new FileWriter(filePath + fileName);
  try {

   fw.write(args);
  } catch (IOException e) {
   throw e;
  } finally {

   fw.close();
  }
 }

The finally block ensures that the close method is always executed, regardless of whether an exception is issued within the try block. Therefore, you can be sure that the close method will always be called before exiting the method. This way you can be sure that the FileWriter object is closed and that you are not leaking resources.

Finalize:

According to the Java language specification, the JVM guarantees that the object is not reachable until the finalize function is called, but the JVM does not guarantee that the function will be called. In addition, the specification guarantees that the finalize function will run at most once.

Finalize is typically used for the release of some hard-to-control and very important resources, such as I/O operations, data connections. The release of these resources is critical to the overall application. In this case, the programmer should focus on managing (including releasing) these resources through the program itself, supplemented by a finalize function that releases resources, creating a double-play management mechanism, rather than relying solely on finalize to release resources.

conclusion

Final-modifier (keyword) if a class is declared final, it cannot be subclassed or inherited as a parent. So a class cannot be declared both abstract and final. Declaring variables or methods as final ensures that they are not changed in use. Variables that are declared final must be given an initial value at the time of declaration, and can only be read in later references, not modified. Methods that are declared final are also available only and cannot be overloaded.
 
Finally - provide a finally block to perform any cleanup when an exception is handled. If an exception is thrown, the matching catch clause is executed, and control goes into a finally block, if any.  

Finalize - method name. Java technology allows you to use the finalize() method to do the necessary cleanup before the garbage collector clears the object from memory. This method is called on the object by the garbage collector when it determines that the object is not referenced. It is defined in the Object class, so all classes inherit it. A subclass overrides the finalize() method to tidy up system resources or do other cleanup. The finalize() method is called on an object before it is deleted by the garbage collector.


 


Related articles: