Java 7 Usage of #

Source: Internet
Author: User
Tags closure event listener

Do you know "#"? Let me guess, not too familiar. Because in the Java1.4.2 era, "#" is used only in writing the annotated content of Javadoc, to link (@see) to the corresponding class of concrete methods. In addition, we almost never use it when writing code.

However, don't be surprised, after the Java 7 release, "#" may become the most familiar friend of Java programmers! Because a few days ago (Edit note: This translation at the end of November 09, when Sun just announced that Java 7 will contain simple closures), Sun Java Mark Reinhold, chief engineer of SE and OPENJDK, has just announced that Java 7 will add a simplified version of the "Closure" feature, where the key symbol is "#". For this reason, the official release time for Java 7 will be postponed from February 2010 to September.

In Java 7, "#" will let the function method become a first-class citizen of the Java language. Let's look at how to implement the closure with the # syntax:

  Simple Method Reference Example:

For example, to refer to the Equals method of the string class, you should write this:

String#equals (Object)

Look at one more example:

Java.io.file#exists ()

  Event Listener 1 (code example, from Cay Horstmann)

Before using closures:

Button.addactionlistener (

New ActionListener () {

public void actionperformed (ActionEvent) {

System.out.println ("hi!"); }

}

);

After using closures:

Button.addactionlistener (# (ActionEvent e) System.out.println ("hi!)");

Event Listener 2 (code example, from FSM)

Before using closures:

public void init () {

JButton button = ...;

Button.addactionlistener (new ActionListener () {

public void actionperformed (ActionEvent ev) {

Handleaction (EV);

}

});

}

public void handleaction (ActionEvent ev) {

Handle Event

}

After using closures:

After using closures:

public void init () {

JButton button = ...;

Button.addactionlistener (This#handleaction (ActionEvent));

}

public void handleaction (ActionEvent ev) {

Handle Event

}

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.