In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the principle and example usage of Java8 default method DefaultMethods. The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian slowly and deeply to study and learn the principle and example usage of Java8 default method DefaultMethods together.
Java 8 introduces a new language feature called Default Methods.
Default methods enable new functionality to be added to the interfaces of libraries and ensure binary compatibility with code written for older versions of those interfaces.
The default method allows you to add new functionality to an existing library interface and ensures binary compatibility with code written with older versions of the interface.
The default method is an implementation method that prefaces the method signature in the interface with the default keyword.
a simple example
interface InterfaceA { default void foo() { System.out.println("InterfaceA foo"); }}class ClassA implements InterfaceA {}public class Test { public static void main(String[] args) { new ClassA().foo(); //Print: "InterfaceA foo" }}
ClassA class does not implement the foo method in the InterfaceA interface. InterfaceA interface provides a default implementation of the foo method, so you can call the foo method of ClassA class directly.
Why have a default method
Prior to java 8, interfaces and their implementation classes were so tightly coupled that when methods needed to be added to an interface, all implementation classes had to be modified accordingly. The default method solves this problem by adding new methods to an interface without breaking the implementation of an existing interface. This provides a way to upgrade older interfaces while maintaining backward compatibility at a time when lambda expressions are emerging as an important feature of the java 8 language.
String[] array = new String[] { "hello", ", "world",};List list = Arrays.asList(array);list.forEach(System.out::println); //This is the default method for the interface added to jdk 1.8
This forEach method is the default method of the interface newly added in jdk 1.8. It is precisely because of the introduction of the default method that it is not necessary to modify all the implementation classes of the Iterable interface just because the forEach method is added to the Iterable interface.
The following code shows the forEach default method in jdk 1.8's Iterable interface:
package java.lang;import java.util.Objects;import java.util.function.Consumer;public interface Iterable { default void forEach(Consumer
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.