Understanding Dynamic Java Usage

Scripts use the Dynamic Java interpreter to execute.

In addition to supporting standard Java statements and expressions, Dynamic Java extends the Java language in the following ways:

  • You can write classes in the script and create instances of them.

  • You can write functions using the Java method declaration syntax. For example:

    int factorial(int i) { 
        if (i <= 0) return 1;
        else return i * fact(i-1);
    }
    intVar2 = factorial(intVar1);
    

    A function cannot be called until after it is defined. The body of the function does not have access to global variables or parameters, only to the function arguments. So, for example, it would be an error to try to reference intVar1 inside the body of factorial.

  • Variables do not need to be declared if the first use of the variable is an assignment. The variable is implicitly declared with the type of the right-hand expression. Thus, the statement:

    s = xxx.toString();

    declares variable s as type String.

  • Import statements can be interspersed with statements in the script, as long as the Import statement occurs before the symbols it imports are used.