Java 8 new feature method references are described in detail

  • 2020-05-19 04:54:23
  • OfStack

Java 8 new feature method reference

For reference we 1 are generally used in the object, and the characteristics of object reference is: different reference objects can operate with 1 block of content!

Java 8's method references define four formats:

Reference the static method ClassName :: staticMethodName Reference object method: Object:: methodName Reference specific type method: ClassName :: methodName Reference constructor: ClassName :: new

Static method reference example


/**
 *    Static method reference 
 * @param <P>  Reference the parameter type of the method 
 * @param <R>  Refers to the return type of the method 
 */
@FunctionalInterface
interface FunStaticRef<P,R>{

  public R tranTest(P p);

}



public static void main(String[] args) {

    /*
    *   Static method reference : public static String valueOf
    *    the String the valueOf()  Method reference is  FunStaticRef#tranTest  methods 
    */
    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>  The reference method returns the type 
 */
@FunctionalInterface
interface InstanRef<R>{

  public R upperCase();
}

public static void main(String[] args) {
  
   /*
   *  A reference to a common method : public String toUpperCase()
   *
   */
   String str2 = "i see you";
   InstanRef<String> instanRef = str2 :: toUpperCase;
   System.out.println(instanRef.upperCase());

}

Examples of specific type method references

A reference to a particular method is difficult to understand. It refers to a common method, but it is referred to in the following way: ClassName :: methodName


 
/**
 *  A reference to a particular method 
 * @param <P>
 */
@FunctionalInterface
interface SpecificMethodRef<P>{
  public int compare(P p1 , P p2);
}

public static void main(String[] args) {

   /*
    *  A reference to a particular method  public int compareTo(String anotherString)
    *   Instead of defining the object before the method reference, it can be interpreted as defining the object on the 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);
}

The constructor references the example


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) {

  /*
   *  Constructor reference 
   */
  ConstructorRef<Book> constructorRef = Book :: new;
  Book book = constructorRef.createObject("Java",100.25);
  System.out.println(book);
}

In general, some of Java 81's new features are not yet heavily used in the projects we're working on, but learn 1 and you won't be surprised to see the code for this new Java 8 feature!

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: