by Walter Nyland
NetBeans 8 is the culmination of years of development work, initially under the leadership of Sun Microsystems and more recently that of Oracle. By consistently positioning NetBeans as the "official IDE for the Java platform," Sun and Oracle have created a unique place for NetBeans, whereby when a new version of Java is released, a related release of NetBeans is automatically pushed out the door. Without tools, Java is unusable and, because competing IDEs have their own separate release cycles, NetBeans continues to lead the pack in being tightly aligned with new releases of Java and being most closely associated as the purest IDE for working with the Java language.
It is for that reason that the 8th version of Java was released hand in hand with the 8th version of NetBeans. As the prime driving force of Java 8 is lambdas, so too underpinning NetBeans 8 are tools simplifying the process of using lambdas in new Java code, as well as tools for the probably even more significant task of migrating existing Java code to make use of lambdas.
What is a lambda? Briefly put, a lambda is an expression separated by an arrow. The parameters are shown on the left, and the method body is shown to its right.
Figure 1: Outline the lambda's structure
Lambdas are very well established in most, possibly even all, modern programming languages and Java was beginning to look a bit stale in their absence. New programmers to Java will pick up the language more quickly if it uses constructs familiar to other common programming languages; hence, the introduction of lambdas helps increase the likelihood of Java adoption amongst the new 'cool kids' in the programming world. Moreover, using lambda expressions instead of the clunky and problematic anonymous inner classes inherent to Java makes for much cleaner code.
NetBeans 8 automatically recognizes code constructs that can be changed to use lambdas. For example, below you see a yellow lightbulb hint that appears in the sidebar, prompting the user to allow NetBeans to transform the existing code construct, an anonymous innner class with a single abstract method, to use a lambda expression:
Figure 2: Watch for the yellow lightbulb
Here is the result, when the user clicks the yellow lightbulb hint shown above. That is, a lambda had been added to the code, which has been restructured to hold less 'boilerplate code' than before:
Figure 3: The result of clicking the lightbulb
The code is tighter, simpler, modernized, and more idiomatic than it was before. When learning about new constructs, such as lambdas, tools such as these yellow lightbulb hints are essential. Without them, converting code becomes an error-prone process and learning about new code constructs can become a frustrating process.
Aiding the adoption of new code constructs is a criticial task of tool providers such as NetBeans, which is a task it has taken up over the years with aplomb. Because NetBeans is able to identify code segments eligible for conversion to use lambdas, it includes batch processing tools to speed up the conversion process significantly. The user can specify a scope within which NetBeans should perform a search for anonymous inner classes that are able to be converted to use lambda expressions:
Figure 4: Selecting the current file
A scan throughout the specified scope is then performed by NetBeans. The result of the scan is shown in the Refactoring window.
Figure 5: The result of the scan, as shown in the Refactoring window
The user can examine each proposal for transformation. In each case, a 'before' and an 'after' are shown in a Diff window. Where applicable, proposals can be excluded from the transformation and then, when the user clicks "Do Refactoring," all proposals that are included are transformed to use lambdas.
In this way, NetBeans can help migrate extremely large code bases to use lambdas, without causing the user hours of painful rewriting of existing code. Aside from saving time, NetBeans ensures that the process is painless and error-free.
In subsequent articles, we will look at method references and the new Streams API, both of which have been introduced in Java 8, in the context of the tools that are provided by NetBeans 8 for working with these new constructs and APIs. The NetBeans IDE is free and open sources and can be downloaded at netbeans.org.