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:
- { and } are always on their own lines
- The standard indent is 2 spaces
- Indentation for the standard syntax is as shown in the reference and examples
- switch statements always have a default value
- switch drop throughs are always documented with a comment
- The ternary operator (?:) is not used
- The action assignment operators (+=, -= etc) are not used
Operators and expressions
Basic operators
Symbols | Operations |
x + y | Addition (if both are numbers), or String concatenation |
x - y, x * y, x / y | Subtraction, Multiplication, Division |
x = y | Assignment |
++x, x++ | Pre increment, Post increment |
--x, x-- | Pre decrement, Post decrement |
-x | Negation |
x & y | Bitwise AND (12 & 4 is 4) |
x | y | Bitwise OR (12 | 4 is 12) |
x ^ y | Bitwise XOR (12 | 4 is 8) |
~x | Bitwise NOT (~12 is -13) |
x < y, x <= y | Less than, Less than or equal |
x > y, x >= y | Greater than, Greater than or equal |
x == y, x != y | Equal to, Not equal to |
!x | Logical NOT |
x && y | Logical AND (true && true is true) |
x || y | Logical 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);