Seven new features of jdk8 Nashorn JavaScript

Posted by ASDen on Wed, 08 Apr 2020 09:42:33 +0200

Introduction

Nashorn is a JavaScript engine. Starting from JDK 1.8, nashorn replaced Rhino (JDK 1.6, JDK 1.7) as an embedded JavaScript engine of Java. Nashorn fully supports the ECMAScript 5.1 specification and some extensions. It uses new language features based on JSR 292, including invokedynamic introduced in JDK 7, to compile JavaScript into Java bytecode. This results in a performance improvement of 2 to 10 times over the previous rhino implementation.

What's new in java8

  1. Lambda expression, one of the new features of jdk8
  2. Method reference of the second new feature of jdk8
  3. Three function interface of new characteristics of jdk8
  4. Four default methods for new features of jdk8
  5. Five new features of jdk8 Stream API
  6. Six Optional classes of new features of jdk8
  7. Seven new features of jdk8 Nashorn JavaScript
  8. New features of jdk8 date time API
  9. Nine new features of jdk8 Base64

jjs

jjs is a command-line tool based on the Nashorn engine. It takes some javaScript source code as parameters and executes the source code. For example, we create a sample.js file with the following contents:

print('Hello World!');

Open the console (navigate to the jdk/bin directory under windows), and enter the following command:

jjs sample.js

The output is:

Hello World!

jjs interactive programming

Open the console (navigate to the jdk/bin directory under windows), and enter the following command:

C:\Program Files\Java\jdk1.8.0_162\bin>jjs
jjs> print("Hello,World!")
Hello,World!
jjs> quit()

Transfer parameters

Open the console (navigate to the jdk/bin directory under windows) and enter the following command:

C:\Program Files\Java\jdk1.8.0_162\bin>jjs -- a b c
jjs> print('Letter:'+arguments.join(", "))
//Letters: a, b, c
jjs>

Calling javaScript in java

Using ScriptEngineManager, JavaScript code can be executed in Java. The example is as follows:

package com.adanblog.demo;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

/**
 * java8 Seven new features: Nashorn JavaScript
 * @author www.adanblog.com
 *
 */
public class NashornJavaScriptTest {
	public static void main(String[] args) {
		ScriptEngineManager scriptEngineManager=new ScriptEngineManager();
		ScriptEngine scriptEngine=scriptEngineManager.getEngineByName("nashorn");
		String name="Runoob";
		Integer result=null;
		try {
			scriptEngine.eval("print('" + name + "')");
			result=(Integer)scriptEngine.eval("3+3");
		} catch (Exception e) {
			System.out.println("Script execution error: "+ e.getMessage());
		}
		System.out.println(result.toString());
	}
}

The operation output is:

Runoob
6

Calling Java in JavaScript

Add the following code to the sample.js file:

var BigDecimal = Java.type('java.math.BigDecimal');

function calculate(amount, percentage) {

   var result = new BigDecimal(amount).multiply(
   new BigDecimal(percentage)).divide(new BigDecimal("100"), 2, BigDecimal.ROUND_HALF_EVEN);
   
   return result.toPlainString();
}

var result = calculate(568000000000000000023,13.9);
print(result);

Open the console (navigate to the jdk/bin directory under windows), and enter the following command:

C:\Program Files\Java\jdk1.8.0_162\bin>jjs sample.js
Hello World!
78952000000000002017.94

About Adan blog

The above content is just one family's opinion. Due to limited personal ability, there are inevitably omissions and mistakes. If you find a bug or have better suggestions, you are welcome to comment and correct. Thank you very much. Here's Adan's personal blog, which records all the blogs in study and work. Welcome to visit.

Personal blog: http://www.adanblog.com/

GitHub: https://github.com/adanblog/

Topics: Programming Java Javascript JDK Windows