When Java 8 ships on March 18, it will come equipped with a new JavaScript engine codenamed Nashorn. Created by Oracle, Nashorn enables Java developers to embed JavaScript in their applications and develop standalone JavaScript apps, all in the Java Virtual Machine.
JavaScript development on the JVM isn’t a new concept. Dating back to 1997, Netscape and then Mozilla developed and maintained the original JVM-based JavaScript engine, known as Rhino. Using the jrunscript command-line tool, Nashorn (pronounced “nas-horn,” the German word for rhinoceros) leverages new native APIs such as MethodHandles and InvokeDynamic to implement a lightweight, high-performance JavaScript runtime in Java.
At a presentation entitled “Java and JavaScript—Shaken, Not Stirred,” hosted by the New York Java Special Interest Group, Farata Systems senior software engineer Viktor Gamov explained how Nashorn aims to bridge the gap between Java and JavaScript.
“Nashorn is based off two worlds,” he said. “We can take the features we love in Java and use libraries from the JavaScript community.”
Gamov explained that for Java developers, JavaScript is like an iceberg. On top is a deceptively familiar syntax, but a completely unknown language awaits underneath. For Java developers who prefer to work in an environment in which they’re comfortable, Nashorn provides extension points for an application—the bridge between the two—to use JavaScript within the JVM for Web content generation.
(Related: JavaScript tops RedMonk’s rankings)
Nashorn is the pure JavaScript implementation in the JVM, 100% written in Java and fully compatible with ECMAScript 5.1, the most recent standardized version of JavaScript.
“Compared to Rhino, Nashorn actually compiles JavaScript into byte code,” Gamov said. “You can add a new query with no compilation, and easily modify it or introduce new logic without recompilation.”
Oracle’s new JavaScript engine also borrows several features from Google Chrome’s V8 engine, using the same UI as the Chrome debugger and the same testing suite as V8. Gamov also pointed out that while Java templating is done on the server-side, today’s browser performance can execute JavaScript fast enough to do templating on the client-side.
“I think there’s some perception out that Java is not as good anymore, but Java 8 will again make Java awesome,” Gamov said. “I can’t tell you to code Java or [a competing platform like] Node.js. But if I’m a developer who wants to stay in my comfort zone with a little bit of spice, which is the JavaScript here, Nashorn provides that.”
Nashorn is fully open-source, and its documentation is available at OpenJDK.