Execute Javascript inside Java application

ScriptEngineManager:

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

public class JavascriptExecuter {
    public static void main(String[] args){
        ScriptEngineManager mgr = new ScriptEngineManager();
        ScriptEngine engine = mgr.getEngineByName("JavaScript");
        //Boolean return
        String query = "1==1";
        try {
            System.out.println(engine.eval(query).getClass());
            Boolean val = (Boolean) engine.eval(query);
            System.out.println(val);
        } catch (ScriptException e) {
            e.printStackTrace();
        }

        //Double return
        query = "var sum = 0; var arr = [1, 2, 3, -5]; for(i=0; i<arr.length; i++){ sum+=arr[i]; };";
        try {
            System.out.println(engine.eval(query).getClass());
             Double val = (Double) engine.eval(query);
            System.out.println(val);
        } catch (ScriptException e) {
            e.printStackTrace();
        }

        //String return
        query = "var string = 'something'; string.replace('thing', 'things')";
        try {
            System.out.println(engine.eval(query).getClass());
            String val = String.valueOf(engine.eval(query));
            System.out.println(val);
        } catch (ScriptException e) {
            e.printStackTrace();
        }

        //Object return
        query = "var string = 'something'; string.replace('thing', 'things')";
        try {
            System.out.println(engine.eval(query).getClass());
            Object val =engine.eval(query);
            System.out.println(val);
        } catch (ScriptException e) {
            e.printStackTrace();
        }

        //send variables to engine
        query = "string.replace(string1, string2)";
        try {
            engine.put("string", "something");
            engine.put("string1", "thing");
            engine.put("string2", "nothing");
            System.out.println(engine.eval(query).getClass());
            String val = String.valueOf(engine.eval(query));
            System.out.println(val);
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }
}