Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences of final,finally,finalize in Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the differences of final,finally,finalize in Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the differences between final,finally,finalize in Java"?

1.final introduction

Final is a keyword in the Java language. Objects decorated with final are not allowed to modify or replace their original values or definitions.

Final can be used to modify: classes, methods, variables, and parameters, which can be used to modify the "parameter" item, which is easy to be forgotten. These are the four uses of final.

1.1 final usage instructions

When final modifies a class, the class is not allowed to be inherited, indicating that the design of the class is perfect and does not need to be modified or extended.

When final modifies a method, this method does not allow any classes that inherit from this class to override the method, indicating that the functionality provided by this method meets the current requirements and does not need to be extended.

When final modifies a variable, it means that the variable cannot be modified once it is initialized.

When final modifies a parameter, it indicates that the parameter is not allowed to be modified throughout the method.

1.2 demonstration of final usage

Final decorating class:

Final class Animal {}

Final modification method:

Public class FinalExample {public final void sayHi () {System.out.println ("Hi~");}}

Final modifier variable:

Public class FinalExample {private static final String MSG = "hello"; / /.}

Final modification parameters:

Introduction to public class FinalExample {public void sayHi (final String name) {System.out.println ("Hi," + name);}} 2.finally

Finally is a mechanism in Java that ensures that key code must be executed.

We can use try-finally or try-catch-finally to do things like closing the JDBC connection, ensuring that the lock is released, and so on.

2.1 finally usage shows try {/ / do something} finally {/ / the code that will be executed} 2.2 finally extension

Sometimes the interview will ask: will finally be executed? This is a suspected inducement problem. Under normal circumstances, finally will certainly be executed, but there is a special case that finally will not be implemented.

The special implementation code and execution results are as follows:

3.finalize introduction

Finalize is a basic method in the Object class, which is designed to ensure that the object completes the collection of specific resources before being garbage collected, but has been marked as a deprecated method (deprecated) in JDK 9.

The finalize method is not recommended in actual development. Although it is created, there is no guarantee that the finalize method will be executed, so do not rely on it to release any resources, because its execution is extremely unstable. Discarding it in JDK 9 is also a good proof of this point.

3.1finalize performance issu

In addition to performing "instability", finalize also has some performance problems.

Because the execution of finalize is associated with garbage collection, once the non-empty finalize method is implemented, it will cause the corresponding object collection to become slower in order of magnitude. Someone has done benchmark, which is about 40 times lower than 50 times.

Because finalize is designed to be called before the object is garbage collected, this means that the object that implements the finalize method is a "special citizen" and JVM has to do extra processing on it. Finalize is essentially a hindrance to rapid recycling, which can cause your object to go through multiple garbage collection cycles before it can be recycled.

At this point, I believe you have a deeper understanding of "what are the differences between final,finally,finalize in Java?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report