You’ve probably read or heard about some of the key features of the upcoming Java SE 8 and JDK 8 already:
- Project Lambda (JSR 335), which makes it easier to write code for multi-core processors,
- a set of Compact Profiles, which allow Java SE 8 implementations to scale down easily,
- a new Date and Time API (JSR 310), and
- the removal of the “permanent generation” from the HotSpot JVM.
By late 2012, there was a Nashorn Project in the OpenJDK community, and the code making up the implementation was open source. It attracted early adopters and rapidly improved to the point of passing all of ECMAScript compliance tests. Nashorn eventually replaced Rhino during the development of the upcoming JDK 8 release in 2013, and has evolved into one of the key features of that release.
The open source code that Nashorn is composed of can be found in the OpenJDK Project Nashorn Mercurial repositories.
Compatibility matters, as Java developers know very well. The ECMAScript 5.1 standard has a corresponding test suite, available at http://test262.ecmascript.org. Nashorn passes all the compliance tests, as compatibility has been a primary goal of the implementation from the beginning.
Under the hood, Nashorn uses the invokedynamic JVM instruction to implement all of its invocations. That is a major component of the comparative improvement of performance and memory usage regarding Mozilla Rhino.
You can use string interpolation
to use the values of variables or expressions to construct strings
within double quotation marks. You can also use a here document
(heredoc) to specify strings that preserve line breaks and
indentation. Environment variables, command line arguments, output
and error strings are available as global objects, as well as the
script’s exit code and a global function to run commands.
Additionally, Nashorn provides several built-in functions to exit the script, print and read lines from standard output and input, read files, load scripts, and bind properties of objects.
Finally, access to the JavaFX primary stage is provided through a global property.