Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Jump into JavaFX, Part 2: JavaFX Script

Scripting in JavaFX: From language fundamentals to data binding

  • Print
  • Feedback

Page 4 of 5

Operators in JavaFX Script

Many expressions consist of operands that are manipulated by operators. JavaFX Script offers operators found also in Java plus a few operators that are unique to the newer language. Table 1 presents the list of operators found in the JavaFX Script language reference document (draft) -- note that operators are grouped by precedence with the highest precedence group at the top.

Table 1. Operator precedence

Priority Operator Description
1 function()
()
new
class name { attribute initializers }
JavaFX function
Expression in brackets
Instantiate a new object
Instantiate and initialize a new object via an object literal
2 ++ (suffixed)
-- (suffixed)
Post-increment assign
Post-decrement assign
3 ++ (prefixed)
-- (prefixed)
not
sizeof
reverse
indexof
-->
Pre-increment assign
Pre-decrement assign
Logical negation
Size of a sequence
Reverse a sequence
Index of a sequence element
Tween
4 *
/
mod
Multiplication
Division
Remainder -- you can also use % in the Preview SDK, but shouldn't
5 +
-
Addition
Subtraction
6 ==
!=
<
<=
>
>=
Equality
Inequality
Less than
Less than or equal to
Greater than
Greater than or equal to
7 instanceof
as
Type checking
Cast
8 or Logical OR
9 and Logical AND
10 +=
-=
*=
/=
%=
Add and assign
Subtract and assign
Multiply and assign
Divide and assign
Remainder and assign -- this operator won't be present in SDK 1.0
11 = Assignment

Although not appearing in the table, I also consider insert and delete to be operators.

Conditional expressions

JavaFX Script lets you create some interesting expressions. For example, you can take advantage of the if-based conditional expression to assign one of two values to a variable during its declaration. This makes the conditional expression similar to Java's (a < b) ? a : b construct, as demonstrated in the following script:

Listing 3. If-then-else expression

var age = 65;
var receivingPension = if (age >= 65) then true else false; // then is optional
java.lang.System.out.println (receivingPension) // Output: true

In this example, a conditional expression determines if an individual is eligible to receive pension based on a retirement age of 65. Of course, a person cannot receive a pension if they're dead. Therefore, to make the previous script somewhat more accurate, the following script uses a Boolean expression to make sure that the person receives pension if they're 65 or older, and not also dead:

Listing 4. Boolean expression

var age = 65;
var dead = true;
var receivingPension = if (age >= 65 and not dead) true else false;
java.lang.System.out.println (receivingPension) // Output: false

Note that JavaFX Script replaces Java's &&, ||, and ! operators with its more readable and, or, and not equivalents. This makes scripts less confusing to those who are not familiar with Java. JavaFX Script also makes other operator-oriented changes, which are described in the aforementioned language reference document.

If the conditional expression doesn't return a value, it cannot be assigned to a variable. For example, var a = 1; var b = if (a == 1) then System.out.println ("a == 1") else System.out.println ("a != 1"); doesn't compile because System.out.println() doesn't return a value that can be assigned to b.

There's something else you need to know about conditional expressions: The expression following the conditional expression's then-part or else-part must be surrounded with braces (it must be a block) if the expression begins with a keyword. For example, braces are required in var a = 1; if (a == 1) then { var b = 2 } because the expression following the then-part begins with keyword var.

Loop expressions

For iteration-based logic, JavaFX Script supports while-based and for-based loop expressions. The former loop expression is practically identical to its equivalent Java statement, except for JavaFX Script requiring this expression's body to always be delimited with brace characters. The following script demonstrates a simple while-based loop expression:

Listing 5. While-based loop expression

var i = 0;
while (i < 10) { java.lang.System.out.println (i++) }

The for-based loop expression is similar to the enhanced for loop introduced by J2SE 5.0. This loop expression iterates over the elements from JavaFX Script's only data structure, the array-like sequence (an ordered list of objects). Although I discuss sequences in more detail later, the following script illustrates a sequence being accessed via this expression:

Listing 6. For-based loop expression

var nums = [5, 7, 3, 9];
var avg = 
{
     var sum = 0;
     for (a in nums) sum += a;
     sum / sizeof nums
}

java.lang.System.out.println ("Average is {avg} ") // Output: Average is 6

This script calculates and outputs the average of an integers sequence, which is specified as a comma-delimited list of integers surrounded by square brackets. It demonstrates the JavaFX Script capability for assigning a block of variable declarations and expressions -- a block expression -- to a variable. The value of this block expression's final expression, sum /= sizeof nums, is assigned to avg.

The sizeof operator

The sizeof operator returns the number of elements in its sequence operand. If the operand is null, sizeof returns 0.

Loop control expressions

To break out of a loop, or to continue with the next loop iteration, you can use JavaFX Script's break and continue expressions. Although they support the same break and continue keywords as their Java counterparts, these expressions do not support labels. The following script demonstrates the break expression (the braces surrounding break are required):

Listing 7. Break expression

var x = [1..10];
for (a in x)
{
    java.lang.System.out.println (a);
    if (a == 5) { break }
}

Range expressions

Instead of using a comma-delimited list of integers, the previous script declares a sequence via a range expression. This expression uses two integers separated by .. notation to identify a list of values forming an arithmetic series. Although the default interval between successive values is 1, this interval can be changed by specifying the step keyword followed by a different interval value:

Listing 8. A pair of range expressions

java.lang.System.out.println ([1..10 step 2]);  // a sequence of all the odd integers from 1 through 9
java.lang.System.out.println ([10..1 step -2]) // a sequence of all the even integers from 10 through 2

Return expressions

To return a value from a function, you can use JavaFX Script's return expression, which uses the same return keyword as its Java counterpart. However, this keyword isn't required; you can specify an expression by itself, and its value will return from the function. The following script demonstrates this capability:

  • Print
  • Feedback

Resources

 

More from JavaWorld