Language Reference

This is an introduction to RedwoodScript (Java) syntax. The interpreter supports more syntax than is listed here, this only covers the basics required to get started. Other references provide far more detail. You can find more information about the Java syntax in Wikipedia: Java Syntax.

Code conventions

The code below uses Redwood standard conventions for RedwoodScript. In particular:
  1. { and } are always on their own lines
  2. The standard indent is 2 spaces
  3. Indentation for the standard syntax is as shown in the reference and examples
  4. switch statements always have a default value
  5. switch drop throughs are always documented with a comment
  6. The ternary operator (?:) is not used
  7. The action assignment operators (+=, -= etc) are not used

Operators and expressions

Basic operators

SymbolsOperations
x + yAddition (if both are numbers), or String concatenation
x - y, x * y, x / ySubtraction, Multiplication, Division
x = yAssignment
++x, x++Pre increment, Post increment
--x, x--Pre decrement, Post decrement
-xNegation
x & yBitwise AND (12 & 4 is 4)
x | yBitwise OR (12 | 4 is 12)
x ^ yBitwise XOR (12 | 4 is 8)
~xBitwise NOT (~12 is -13)
x < y, x <= yLess than, Less than or equal
x > y, x >= yGreater than, Greater than or equal
x == y, x != yEqual to, Not equal to
!xLogical NOT
x && yLogical AND (true && true is true)
x || yLogical OR (true || false is true)

Variables and assignment

To create a variable (eg. of type int):
int x;
To create a variable and assign a value:
int x = 1;

Arrays

To create a new array (eg. of type Job) with size elements:
Job [] anArray = new Job[size];
To create a new array with elements expression1, expression2, expression3:
int [] anArray = new int [] {expression1, expression2, expression3};

Conditionals

if .. else

if (logical expression)
{
  statements;
}
else if (logical expression)
{
  statements;
}
else
{
  statements;
}

switch ... case

switch (expression)
{
  case value1:
    statements;
    break;
  case value2:
    statements;
    break;
  default:
    statements;
    break;
}
You should always put a default case in, even if you think it will never be reached, as it makes it clear what happens if invalid or unexpected input is entered. You should always add break statements, even on the last (default) case. You should always add a comment if you use the drop through behavior (no break statement). For example:
switch (i)
{
  case 1:
    // Drop through
  case 2:
    out.println("You entered " + i);
    break;
  default:
    out.println("Invalid input");
    break;
}

Loops

For loop

for (initial expression; conditional expression; loop expression)
{
  statements;
}
For example, to print the numbers 0 through 9:
for (int i = 0; i < 10; i++)
{
  jcsOut.println(i);
}
To loop over an iterator (eg. from JobDefinition.getJobDefinitionParameters()):
JobDefinition jd = jcsSession.getJobDefinitionByName("System_OperatorMessage");
for (Iterator it = jd.getJobDefinitionParameters(); it.hasNext(); )
{
  JobDefinitionParameter jdPar = (JobDefinitionParameter) it.next();
  jcsOut.println(jdPar.getName());
}
You can use the for statement with queries. Example 1: hold all jobs with JobId > 10:
for (Iterator it = jcsSession.executeObjectQuery("select Job.* from Job where JobId > ?", new Object [] { Integer.valueOf(10) }); it.hasNext(); )
{
  Job job = (Job) it.next();
  job.hold();
}
jcsSession.persist();
` Example 2: To print the job id of all jobs in the database.
for (Iterator it = session.executeObjectQuery("select Job.* from Job", null); it.hasNext(); )
{
  Job job = (Job) it.next();
  jcsOut.println(job.getJobId());
}
`

while loop

while (conditional expression)
{
  statements;
}

do .. while loop

do
{
  statements;
}
while (conditional expression);