Java 8 new Feature method reference detailed introduction _java

Source: Internet
Author: User

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:

    1. Referencing static methods ClassName:: Staticmethodname
    2. Reference object method:: MethodName
    3. Referencing specific type methods: ClassName:: methodname
    4. 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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.