Manufacturers interviewer asks you know final, finally, finalize what is the difference?

Preface
Java programmer interview, the foundation is really important. This is the basis of things, companies are valued, especially large companies, they see human potential, they are willing to spend the energy to train, so the foundation is the most important. Before a lot of people ask me how to do a small project experience, then go to lay a solid foundation, the basis of good when you made that when your other things are not important.

The Java language has a lot of look similar, but they use a completely different language elements, the content is often easy to become an interviewer visits your level of knowledge to master the entry point.
Today, I want to ask you is a classic Java basic questions, talk about the final, finally, finalize what is the difference?
Manufacturers interviewer asks you know final, finally, finalize what is the difference?
A typical answer
final can be used to decorate classes, methods, variables, each with a different meaning, represents the final modified class can not inherit expansion, final variables can not be amended, and the final method is not overridden (override ).
finally it is a mechanism to ensure that the focus of Java code that must be executed. We can use the try-finally or try-catch-finally close a JDBC connection to similar, to ensure unlock lock operation.
finalize is a base class java.lang.Object method, which is designed to ensure complete recovery of specific target resource before being garbage collected. finalize the mechanism has now been deprecated in JDK 9 and began to be marked as deprecated.

Analysis of test centers
this is a very classic Java basic question, I answer above mainly from the perspective of grammar and usage practices, in fact, there are many ways you can explore in depth, the interviewer can also examine your performance, concurrency, object lifecycle understand the basic aspects of the garbage collection process or the like.

For the final
recommended final keyword to clear semantics, logical intent of our code, which has been proven in many scenes is very good practice, such as:
We can declare a method or class final, so that you can clearly inform others these actions are not allowed to modify.
If you are concerned about the definition of Java core libraries or source code, you have not found a lot of class java.lang package below, a considerable portion are declared to be final class? The same is true in some base class of third-party libraries, which can effectively avoid the user to change the underlying API function, to some extent, it is a necessary means to ensure the safety of the platform.
Use final modification of parameters or variables can also be assigned clearly to avoid accidents caused by programming errors, even, it was explicitly recommended that all method parameters, local variables, member variables declared as final.
final variable produces a certain degree of immutable (immutable) effect, therefore, can be used to protect read-only data, especially in concurrent programming, because clearly no longer assign final variables, it helps to reduce the additional synchronization overhead, also save some defensive copies necessary.
final may have performance benefits of many articles or books are introduced to improve performance in certain scenarios, for example, may contribute to the JVM using the final method inlining, the compiler can improve the ability of conditional compilation and so on.
Frankly, a lot of similar conclusions are based on assumptions drawn in the Union may not rely, such as modern high-performance JVM (such as HotSpot) determine final tips to believe JVM is still very smart. Similarly, the impact on the final field performance, in most cases, need not be considered.
From the perspective of development practice, I do not want to over-emphasize this point, and this is very related to implementation of the JVM, unproven more difficult to grasp. My suggestion is that, in the daily development, unless there are special considerations, otherwise it is best not to expect the benefits of this so-called performance tips to bring the program is best reflected in its semantic purpose. If you really are interested in this area, can access relevant information, I will not go into details, but do not forget to test.

To finally
know exactly how to use enough. We need to close the connection and other resources, but is recommended to use try-with-resources statement added in Java 7, because usually the Java platform better able to handle exceptions, coding amount should be much less, why not do it.

Note
finally exception code will not be executed:
Manufacturers interviewer asks you know final, finally, finalize what is the difference?
Final modifications List, will not affect their behavior:Manufacturers interviewer asks you know final, finally, finalize what is the difference?

final only constraint strList this reference can not be assigned, but strList object behavior is not final affect, adding elements and is completely normal. If we really want the object itself is immutable, you need the appropriate class supports immutable behavior.
In the example above, List.of method is to create immutable List, the last sentence add an exception is thrown at run time.
Java platform currently in use java.lang.ref.Cleaner gradually replace in the original finalize implementation.

For finalize
we must be clear that it is not recommended, the industry practice has repeatedly proved that it is not a good way, in Java 9, the even explicitly Object.finalize () marked as deprecated! If there is no particular reason, do not finalize method to achieve, and do not expect to use it for recycling.
why? Simply put, you can not guarantee what time execution finalize, whether performed in line with expectations. Improper use can affect performance, leading to deadlock, hangs and so on.
In general, the use of try-with-resources or try-finally mechanism mentioned above, is a very good way to recycle resources. If you do need additional treatment, or other mechanisms can be considered Cleaner alternative to Java provides.

Guess you like

Origin blog.51cto.com/14442094/2422397