2. Scripting Languages

2.1. Example (Java with JavaScript)

Java supports scripting via the Nashorn engine or GraalVM.

import javax.script.*;

 

public class ScriptingExample {

    public static void main(String[] args) throws ScriptException {

        ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");

        String script = "var sum = 0; for (var i = 1; i <= 3; i++) { sum += i; } sum;";

        Object result = engine.eval(script);

        System.out.println(result); // Output: 6 (1 + 2 + 3)

    }

}

JavaScript script runs within Java.

Strengths

Fast prototyping.

Ideal for automation.

Rich ecosystems.

Weaknesses

Slower than compiled languages.

Not for systems programming.

Dynamic typing risks errors.