Java seeks closure on closures

 

Connecting state and local government leaders

Closures'blocks of code with unbound variables'could make better use of multicore processors, according to programmers.

SAN FRANCISCO'If there is one topic that seems to get Java programmers excited, it is the question of whether to include a new feature called closures in the next version of Java. Advocates say closures would bring more flexibility to the language, but others worry it might encourage bad coding practices and make the language more difficult to work with.

The debate surfaced again today at the JavaOne conference here, when Neal Gafter, a software engineer at Google, presented his proposed specification for adding closures to Java. The specification, called BGGA, was named after its authors: Gilad Bracha, Gafter, James Gosling and Peter von der Ah'. Gosling is the founder of Java.

Closures are blocks of code with unbound variables, or variables that are not assigned until the closure is called. Although they occur in Haskell and some other functional programming languages that are used relatively rarely, closures are not a feature of Java.

"A closure expression in a program'creates an object. The object wraps up the code that is the body of the closure and anything that is referenced from enclosing scope, such as local variable. It wraps them up and packages them in an implementation of some interface, and that interface is whatever interface type is expected in the context of the program," Gafter said. Closures are sometimes called function objects or anonymous functions.

Closures look different from most Java code. BGGA introduces a new syntactic form for Java, one markedly different from the usual Java format, resembling the often-dense format of functional languages.

Nonetheless, adding BGGA-styled closure functionality to Java 7 would be beneficial in a number of ways, Gafter said. It could potentially reduce the amount boilerplate code a programmer must write. Programmers could write a single closure that could be called from multiple instances by using a short application programming interface. He offered one example of a performance monitoring function, which may be added in selected places in a program. The function collects performance data from in various points in the program and then submits the results to a database. Without closures, the programmer must write much the same code repeatedly. Under closures however, the code could be defined as an application programming interface that could easily be called within the main body of the program.

More important, closures could make better use of multicore processors. Many languages have libraries that can perform actions on aggregated data, such as a list or an array. In Java, however, actions on aggregate bodies, such as sorting or filtering, are usually performed by using a programming loop, which performs the same action sequentially on every member of the set.

"The advantage of using these aggregate operations with closures is that the code can automatically be parallelized. It makes it very easy to take programs written to be sequential and parallelize them just by using a different library," Gafter said.

He offered an example of a simple function that can find the highest grade point average from all the students in a graduating class. A traditional Java program would use a loop to find all the students of a particular year and then compare each student's GPA, keeping the highest GPA. At the end of the process the highest GPA will be obtained. The same function written as a closure would parallelize the process by filtering the students of the year needed and then finding the maximum score. "Closures compute the results all at once," Gafter said.

Despite the fact that heavy hitters such as Gafter and Gosling are backing closures, at least part of the Java community continues to resist the idea. After the main presentation, one attendee asked Gafter if he thought closures introduce unneeded complexity into Java.

"One of the arguments against closures is that it is difficult to write them correctly, and people will abuse them and misuse them," the attendee noted. Another attendee wondered if the closure syntax was difficult to read, which would make debugging the language more difficult.

Earlier this year, Java.net held an informal poll of Java programmers on whether closures should be included in Java 7. More than 61 percent of the respondents voted for some kind of closure, but almost 38 percent indicated they wanted no closures whatsoever in Java.

In addition to BGGA, other closure proposals include First Class Methods and Concise Instance Creation Expressions.

X
This website uses cookies to enhance user experience and to analyze performance and traffic on our website. We also share information about your use of our site with our social media, advertising and analytics partners. Learn More / Do Not Sell My Personal Information
Accept Cookies
X
Cookie Preferences Cookie List

Do Not Sell My Personal Information

When you visit our website, we store cookies on your browser to collect information. The information collected might relate to you, your preferences or your device, and is mostly used to make the site work as you expect it to and to provide a more personalized web experience. However, you can choose not to allow certain types of cookies, which may impact your experience of the site and the services we are able to offer. Click on the different category headings to find out more and change our default settings according to your preference. You cannot opt-out of our First Party Strictly Necessary Cookies as they are deployed in order to ensure the proper functioning of our website (such as prompting the cookie banner and remembering your settings, to log into your account, to redirect you when you log out, etc.). For more information about the First and Third Party Cookies used please follow this link.

Allow All Cookies

Manage Consent Preferences

Strictly Necessary Cookies - Always Active

We do not allow you to opt-out of our certain cookies, as they are necessary to ensure the proper functioning of our website (such as prompting our cookie banner and remembering your privacy choices) and/or to monitor site performance. These cookies are not used in a way that constitutes a “sale” of your data under the CCPA. You can set your browser to block or alert you about these cookies, but some parts of the site will not work as intended if you do so. You can usually find these settings in the Options or Preferences menu of your browser. Visit www.allaboutcookies.org to learn more.

Sale of Personal Data, Targeting & Social Media Cookies

Under the California Consumer Privacy Act, you have the right to opt-out of the sale of your personal information to third parties. These cookies collect information for analytics and to personalize your experience with targeted ads. You may exercise your right to opt out of the sale of personal information by using this toggle switch. If you opt out we will not be able to offer you personalised ads and will not hand over your personal information to any third parties. Additionally, you may contact our legal department for further clarification about your rights as a California consumer by using this Exercise My Rights link

If you have enabled privacy controls on your browser (such as a plugin), we have to take that as a valid request to opt-out. Therefore we would not be able to track your activity through the web. This may affect our ability to personalize ads according to your preferences.

Targeting cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising.

Social media cookies are set by a range of social media services that we have added to the site to enable you to share our content with your friends and networks. They are capable of tracking your browser across other sites and building up a profile of your interests. This may impact the content and messages you see on other websites you visit. If you do not allow these cookies you may not be able to use or see these sharing tools.

If you want to opt out of all of our lead reports and lists, please submit a privacy request at our Do Not Sell page.

Save Settings
Cookie Preferences Cookie List

Cookie List

A cookie is a small piece of data (text file) that a website – when visited by a user – asks your browser to store on your device in order to remember information about you, such as your language preference or login information. Those cookies are set by us and called first-party cookies. We also use third-party cookies – which are cookies from a domain different than the domain of the website you are visiting – for our advertising and marketing efforts. More specifically, we use cookies and other tracking technologies for the following purposes:

Strictly Necessary Cookies

We do not allow you to opt-out of our certain cookies, as they are necessary to ensure the proper functioning of our website (such as prompting our cookie banner and remembering your privacy choices) and/or to monitor site performance. These cookies are not used in a way that constitutes a “sale” of your data under the CCPA. You can set your browser to block or alert you about these cookies, but some parts of the site will not work as intended if you do so. You can usually find these settings in the Options or Preferences menu of your browser. Visit www.allaboutcookies.org to learn more.

Functional Cookies

We do not allow you to opt-out of our certain cookies, as they are necessary to ensure the proper functioning of our website (such as prompting our cookie banner and remembering your privacy choices) and/or to monitor site performance. These cookies are not used in a way that constitutes a “sale” of your data under the CCPA. You can set your browser to block or alert you about these cookies, but some parts of the site will not work as intended if you do so. You can usually find these settings in the Options or Preferences menu of your browser. Visit www.allaboutcookies.org to learn more.

Performance Cookies

We do not allow you to opt-out of our certain cookies, as they are necessary to ensure the proper functioning of our website (such as prompting our cookie banner and remembering your privacy choices) and/or to monitor site performance. These cookies are not used in a way that constitutes a “sale” of your data under the CCPA. You can set your browser to block or alert you about these cookies, but some parts of the site will not work as intended if you do so. You can usually find these settings in the Options or Preferences menu of your browser. Visit www.allaboutcookies.org to learn more.

Sale of Personal Data

We also use cookies to personalize your experience on our websites, including by determining the most relevant content and advertisements to show you, and to monitor site traffic and performance, so that we may improve our websites and your experience. You may opt out of our use of such cookies (and the associated “sale” of your Personal Information) by using this toggle switch. You will still see some advertising, regardless of your selection. Because we do not track you across different devices, browsers and GEMG properties, your selection will take effect only on this browser, this device and this website.

Social Media Cookies

We also use cookies to personalize your experience on our websites, including by determining the most relevant content and advertisements to show you, and to monitor site traffic and performance, so that we may improve our websites and your experience. You may opt out of our use of such cookies (and the associated “sale” of your Personal Information) by using this toggle switch. You will still see some advertising, regardless of your selection. Because we do not track you across different devices, browsers and GEMG properties, your selection will take effect only on this browser, this device and this website.

Targeting Cookies

We also use cookies to personalize your experience on our websites, including by determining the most relevant content and advertisements to show you, and to monitor site traffic and performance, so that we may improve our websites and your experience. You may opt out of our use of such cookies (and the associated “sale” of your Personal Information) by using this toggle switch. You will still see some advertising, regardless of your selection. Because we do not track you across different devices, browsers and GEMG properties, your selection will take effect only on this browser, this device and this website.