This is the fourth day of my participation in the August Text Challenge.More challenges in August

Summary of new Java8 features:

Java 8 Stream Gameplay

Analysis of Lambda expressions in Java 8

preface

Java 8 has been a few years since Oracle released it on March 18, 2014. However, many software developers are still unaware of its features, which may be the main reason for the slow updating of the Basic Java textbooks. Java 8 method references are a new feature of Java 8. Java 8 has been out for a long time, I believe that many people have already used method references, but some people do not know how to use method references, so this article will guide you to start Java method references.

What is a method reference?

A method reference is an existing method or constructor that is used to directly access a class or instance. Method references provide a way to refer to methods without executing them, requiring a target-type context composed of compatible functional interfaces. When evaluated, the method reference creates an instance of the functional interface.

  • Method references refer to a method by its name.
  • Method references can make language construction more compact and less redundant code.
  • Method references use a pair of colons ::.

Method references and Lambda expressions

In Java 8 we can access class constructors, object methods, and static methods using the :: keyword.

type Method references Lambda expressions
Static method reference The name of the class: : staticMethod -> Class name. StaticMethod (args)
Instance method reference inst::instMethod (args) -> inst.instMethod(args)
Object method reference The name of the class: : instMethod (inst,args) -> Class name instMethod(args)
Build method reference The name of the class: : new (args) -> New class name (args)

The use of method references

In order to better demonstrate the use of method references, the basic code is introduced.

@Data @ApiModel class DataInfo{ private String name; private String info; private String welcome; public static DataInfo create( Supplier<DataInfo> supplier) { return supplier.get(); } public static void getDataInfo(DataInfo DataInfo) {system.out.println ("DataInfo: "+ datainfo.toString ()); } public void getWebNameinfo( DataInfo dataInfo) { System.out.println(" WebName " + dataInfo.getName()); } public void getWelcomeInfo() { System.out.println("welcome " + this.toString()); }}Copy the code

Static method reference

Static method references: Its syntax is Class::static_method

dataInfoList.forEach(DataInfo::getDataInfo);
Copy the code

Instance method reference

A method reference to a particular object: its syntax is instance:: Method instance

dataInfoList.forEach( DataInfo.create( DataInfo::new )::getWebNameinfo );
Copy the code

Object method reference

A method reference to an arbitrary object of a particular Class: its syntax is Class::method

 dataInfoList.forEach(DataInfo::getWelcomeInfo);
Copy the code

Build method reference

Constructor reference: its syntax is Class::new.

DataInfo dataInfo = DataInfo.create(DataInfo::new);
Copy the code

conclusion

Java 8 method reference, make the construction of the language more compact and concise, reduce redundant code, let programmers write efficient, clean, concise code.

About the author: [Little Ajie] a love tinkering with the program ape, JAVA developers and enthusiasts. Public number [Java full stack architect] maintainer, welcome to pay attention to reading communication.

Well, thank you for reading, I hope you like it, if it is helpful to you, welcome to like collection. If there are shortcomings, welcome comments and corrections. See you next time.