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 uses of Java final

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

Share

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

This article mainly introduces "what are the uses of Java final". In daily operation, I believe many people have doubts about the use of Java final. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions about "what is the use of Java final?" Next, please follow the editor to study!

"what's the difference between talking about final, finally and finalize?"

This is a very classic question.

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

Finalize is a method of the base class java.lang.Object, which is designed to ensure that objects complete the collection of specific resources before being garbage collected.

Today we focus on final. Years of interview experience tells me that many people actually lose points on final, not on the other two. Final can be used to modify classes, methods, and variables, each with different meanings.

1.final-modified class represents a non-inheritable extension

The method modified by 2.final cannot be overridden (override)

3.final-decorated variables are not modifiable.

If you can give the above answer, at least pass. But this answer hides a lot of information, and the real change here is when final modifies variables.

First of all, the above description is not accurate enough to say that "final-modified variables cannot be modified once assigned."

Take a look at the following code:

Public class MyClass {public MyClass (int foo) {this.foo = foo;} private final foo;}

In this code, the member variable foo of the class MyClass is decorated with final, but it is not assigned a value, but is assigned a value in the constructor.

Of course, you can't assume that "final-modified variables are unmodifiable", but at least it doesn't mean it.

In fact, the design of the Java language makes sense, and the use of this feature can achieve such a goal: when we invent a class, we sometimes find that there are some parameters that need to be passed in, and we hope that once it is determined, we do not want the object to be changed before it is destroyed. In this case, we can define these parameters as member variables of final, and then the outside world can only specify them through the parameters of the constructor.

Can final be used to modify function parameters or internal variables?

The answer is yes. In fact, this behavior is recommended because it ensures that variables are not accidentally modified. And variables and parameters decorated by final can also be passed to anonymous classes, such as:

When Java implements inner classes, it actually makes a copy, instead of directly using local variables, final can prevent data consistency problems here.

There are a lot of articles or data from the outside world that will introduce that final may have performance benefits.

For example, using final may help JVM inline methods, improve the compiler's ability to compile conditionally, and so on.

Frankly speaking, many similar conclusions are based on assumptions. For example, high-performance JVM (such as HotSpot) may not rely on the hint of final to judge inline, but to believe that JVM is still very intelligent.

Similarly, the impact of final variables on performance, in most cases, is not necessary to consider. Don't expect such tricks to bring so-called performance benefits. On the contrary, a more important reason for using final should be to improve the readability of the code and prevent variables from being accidentally modified.

* also emphasize that although final brings immutable effects, it does not mean that this is the case in all cases. For example, final decorating variables of type java.util.List does not prevent the contents of the list from being modified. The actual effect is to prevent the List object pointed to by the variable from being created again. If you need a List that is not allowed to be modified, you can use Collections.unmodifiableList () to get it.

At this point, the study of "what is the use of Java final" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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