Java code executes arithmetic operations and js Code.
Java SE6 supports common scripting languages.
You can execute the script language in java code, use get ("key"), put ("key", "value") to interact with it, and dynamically call the script.
Package com. test; import java. util. list; import javax. script. invocable; import javax. script. scriptEngine; import javax. script. scriptEngineFactory; import javax. script. scriptEngineManager; import javax. script. scriptException; public class Demo {public static void main (String [] args) {ScriptEngineManager manager = new ScriptEngineManager (); // obtain the List of all engine factories <ScriptEngineFactory> list = manager. getEngineFactories (); for (ScriptEngineFactory factory: list) {System. out. println ("supported scripting language" + factory. getNames (); // supported scripting languages [js, rhino, JavaScript, javascript, ECMAScript, ecmascript]} // get common javascript scripts ScriptEngine engine = manager. getEngineByName ("javascript"); try {// execute the script System. out. println (engine. eval ("2 + 3*2"); // 8 System. out. println (engine. eval ("(true | false) & true"); // true // interacts with the script engine. put ("name", "123456"); engine. eval ("var out =''; "+" for (I = 0; I <name. length; I ++) {"+" out = name. charAt (I) + out} "); String name = (String) engine. get ("out"); System. out. println ("name:" + name); // dynamically call the script language (the Invocable interface must be implemented by the script engine) String str = "abcdef"; if (engine instanceof Invocable) {engine. eval ("function str1 (name) {" + "var out =''; "+" for (I = 0; I <name. length; I ++) {"+" out = name. charAt (I) + out} "+" return out;} "+" function str2 (name) {"+" return 123} "); Invocable invocable = (Invocable) engine; try {Object obj = invocable. invokeFunction ("str1", "123456"); System. out. println ("obj:" + obj);} catch (NoSuchMethodException e) {e. printStackTrace () ;}// dynamic implementation Interface engine. eval ("function run () {" + "print ('asynchronous call')}"); Invocable invocable = (Invocable) engine; // implement the Runnable interface Runnable runnable = invocable. getInterface (Runnable. class); // start Thread t = new Thread (runnable); t. start ();} catch (ScriptException e) {e. printStackTrace ();}}}