Java 8 New Attribute method reference
For reference we are generally used in objects, and object references are characterized by: different reference objects can manipulate the same piece of content!
The Java 8 method reference defines four different formats:
- Referencing static methods ClassName:: Staticmethodname
- Reference object method:: MethodName
- Referencing specific type methods: ClassName:: methodname
- Reference construction Method: ClassName:: New
static method Reference Example
/**
* static method Reference
* @param <P> Reference method parameter type
* @param <R> Reference method return type *
* Functionalinterface
Interface funstaticref<p,r>{public
R trantest (P p);
}
public static void Main (string[] args) {
/
* static method reference: public static String valueof
* The valueof () method of the String is referred to as the Funstaticref#trantest method
* *
funstaticref<integer, string> funstaticref = string::valueof;
String str = funstaticref.trantest (10000);
System.out.println (Str.replaceall ("0", "9"));
}
Object Method Reference Example
/**
* Common method Reference
* @param <R> Reference method return type */
@FunctionalInterface
interface Instanref<r >{Public
R uppercase ();
}
public static void Main (string[] args) {
*
* Reference to Common method: Public String touppercase ()
*
/
string STR2 = "I to You";
Instanref<string> instanref = str2:: touppercase;
System.out.println (Instanref.uppercase ());
}
Example of a specific type method reference
The reference to a particular method is more difficult to understand, and it refers to a common method, but the way it is referred to is: ClassName:: methodname
/**
* References to specific methods
* @param <P> * *
@FunctionalInterface
interface specificmethodref<p>{ public
int Compare (P p1, p p2);
}
public static void Main (string[] args) {
*
* * Reference to a specific method public int compareTo (String anotherstring)
* Instead of defining an object before a method reference, it can be understood to define an object on a parameter!
*/
Specificmethodref<string> specificmethodref = String:: compareTo;
System.out.println (Specificmethodref.compare ("A", "B"));
constructorref<book> constructorref = Book:: New;
Book book = Constructorref.createobject ("Java", 100.25);
SYSTEM.OUT.PRINTLN (book);
}
To construct a method reference sample
Class book{
private String title;
private double price;
Public book () {
} public book
(String title,double price) {
This.price = Price;
this.title = title;
}
@Override public
String toString () {return
"book{" + "title= '" + title + ' \ ' + ", price=" + Price + '} ';
}
}
public static void Main (string[] args) {
/
* * Construction Method Reference */
constructorref<book> constructorref = Book:: New;
Book book = Constructorref.createobject ("Java", 100.25);
SYSTEM.OUT.PRINTLN (book);
}
In general, some of the new features of Java 8 have not been used extensively in the current project, but you will not be aware of the code for this Java 8 new feature until you learn it.
Thank you for reading, I hope to help you, thank you for your support for this site!