In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use Java 8 new features Effectively final", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's learn how to use Java 8's new features Effectively final!
preface
Local variables accessed by local inner classes and anonymous inner classes in Java must be final-qualified to ensure data consistency between inner and outer classes. But starting with Java 8, we can add final modifiers by default without them, which was not possible in previous versions of Java 8. Java calls this feature v Effectively final.
Write the following code in Java 7 and Java 8 and run it:
public class Test {
public static void main(String[] args) {
String name = "IT ";
new Runnable() {
@Override
public void run() {
System.out.println(name);
}
}
}
}
Java 7 compiles as follows:
Fig. 1
You can see the code error in the image above, indicating that we must explicitly declare this variable final (the code in the run method is the output name statement, i.e. System.out.println(name);).
Java 8 compiles as follows:
Fig. 2
Fig. 3
Because the final modifier is added by default, non-final variables can be used directly in anonymous inner classes, while final-modified local variables cannot be reassigned, so a compilation error occurs in Figure 3. That is, starting with Java 8, it does not require programmers to explicitly declare local variables accessed as final. As long as the variable is not reassigned.
A nonfinal local variable or method parameter whose value never changes after initialization is effectively final. In Lambda expressions, when using local variables, it is also required that the variable must be final, so effectively final is very useful in the context of Lambda expressions.
Lambda expressions are frequently used in programming, while anonymous inner classes are rarely used. So, do we explicitly define final for every local variable used in Lambda programming? Obviously that's not a good idea. Java 8 introduces the concept of effectively final.
In fact, the rules have not changed, Lambda expressions and anonymous internal class access to local variables must be final, just do not need programmers to explicitly declare variables as final, thus saving time!
At this point, I believe that everyone has a deeper understanding of "how to use Java 8 new features Effectively final", may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.