Unlike the Java™ programming language, the JavaFX™ Script programming language is an expression language. All executable statements are expressions that consist of zero or more inputs followed by zero (or one) outputs. This includes conditionals, loops, and even blocks.
The following demo provides an example:
import java.lang.Math; import java.lang.System; var rand = (Math.random() * 100).intValue(); var s:String = null; if (rand % 2 == 0) { s = "rand is even" } else { s = "rand is odd" }; System.out.println(s);
A block expression consists of a list of statements (which can be declarations or expressions) surrounded by curly braces and separated by semicolons. If the last statement is an expression, the value of a block expression is the value of the last expression; otherwise the block expression has Void
type.
The following block expression adds a few numbers and stores
the result in a variable named total
:
import java.lang.System; var nums = [5, 7, 3, 9]; var total = { var sum = 0; for (a in nums) { sum += a }; sum; } System.out.println("Total is {total} ");
Range expressions define a sequence of values that form an arithmetic series using the following syntax:
[number1..number2]
Such an expression defines a sequence that contains elements consisting of the integers from number1
to number2
(inclusive).
A simple example of a range expression could be the following:
import java.lang.System; var nums = [0..3]; System.out.println(nums == [0,1,2,3]); // prints true
By default the interval between the values is 1 but you also can specify a different interval. For example, the following expression defines a sequence consisting of the odd numbers between 1 and 10:
[1..10 step 2]
To create a descending range, make sure the second value is less than the first, and specify a negative step value:
import java.lang.System; var nums = [3..0 step -1]; System.out.println(nums == [3,2,1,0]); // prints true
Note that the following declarations actually declare empty sequences:
var nums1 = [3..0 ]; var nums2 = [3..0 step 1];
The if
expression is similar to if
in the Java™ programming language:
if (condition1) { System.out.println("Condition 1"); } else if (condition2) { System.out.println("Condition2"); } else { System.out.println("not Condition 1 or Condition 2"); }
The Java programming language contains both an if
expression
and a conditional expression: a < b ? a : b
.
The JavaFX Script programming language if
expression takes the place of both.
The return
Expression
is the same as found in the Java programming language:
function add(x, y) { return x + y; }
The throw
expression is similar to that of
the Java Programming language, but
only objects extending
java.lang.Throwable
may be thrown and caught:
import java.lang.Exception; function foo() { throw new Exception("this is a java exception"); } function bar() { throw "just a string"; }
The try
and catch
expressions
are similar to those of the Java programming language, but
use JavaFX Script programming language variable declaration syntax:
try { throw "Hello"; } catch (s:String) { System.out.println("caught a String: {s}"); } catch (any) { System.out.println("caught something not a String: {any}"); } finally { System.out.println("finally..."); }
The break
and continue
expressions are
similar to those of the Java Programming language, but labels are
not supported.
Examples:
import java.lang.System; function foo() { for (i in [0..10]) { if (i > 5) { break; } if (i mod 2 == 0) { continue; } System.out.println(i); } } function bar() { var i = 0; while (i < 10) { if (i > 5) { break; } if (i mod 2 == 0) { continue; } System.out.println(i); i += 1; } }
The JavaFX Script programming language provides standard operators similar to those found in the Java programming language. The following chart lists these operators by precedence, comparing to their Java programming language equivalent.
Priority | JavaFX Script Programming Language Operator | Operation | Java Programming Language Operator | Order of Evaluation | |
---|---|---|---|---|---|
1 | function() | JavaFX function | function() | Class | |
() | An expression in brackets | n/a | |||
new | Instantiate a new object | Class | |||
{Object literal} | Instantiate and initialize a new object | Class | |||
2 | ++ (suffixed) |
Post-increment assign | ++ |
Right to Left | |
-- (suffixed) |
Post-decrement assign | -- |
|||
3 | ++ (prefixed) |
Pre-increment assign | ++ |
Right to Left | |
-- (prefixed) |
Pre-decrement assign | -- |
|||
not | Logical negation | <> | Boolean | ||
sizeof | Size of a sequence | Sequence | |||
reverse | Reverse of a sequence | ||||
indexof | Index of a sequence element | ||||
--> | tween | ||||
4 | * |
Multiplication | * |
Left to Right | |
/ |
Division | / |
|||
mod |
Remainder | % |
|||
5 | + |
Addition | + |
Left to Right | |
- |
Subtraction | - |
|||
6 | == |
Equality | == |
Left to Right | |
!= |
Inequality | != |
|||
< |
Less than | < |
|||
<= |
Less than or equal | <= |
|||
> |
Greater than | > |
|||
=> |
Greater than or equal to | => |
|||
7 | instanceof |
Type checking | instanceof |
Class | |
as | Cast | Class | |||
8 | or |
Logical OR | || |
Right to Left | |
9 | and |
Logical AND | && |
Right to Left | |
10 | += |
Add and assign | += | ||
-= |
Subtract and assign | -+ |
|||
*= |
Multiply and assign | *= |
|||
/= |
Divide and assign | /= |
|||
%= |
Remainder and assign | %= |
|||
11 | = |
Assignment | = |
(all) |
Examples:
import java.lang.System; import java.lang.Math; var x = 2; var y = 4; var a = true; var b = false; System.out.println(x == y); // prints false System.out.println(x <> y); // prints true System.out.println(x < y); // prints true System.out.println(x > y); // prints false System.out.println(x >= y); // prints false System.out.println(x <= y); // prints true System.out.println(x + y); // prints 6 System.out.println(x - y); // prints -2 System.out.println(x * y); // prints 8 System.out.println(x / y); // prints 0 System.out.println(x % y); // prints 2 System.out.println(a and b); // prints false System.out.println(a or b); // prints true System.out.println(not a); // prints false System.out.println(sizeof [x,y]); // prints 2 System.out.println([x,y][e | indexof e == 0]); // prints [ 2 ] System.out.println(if (a) x else y); // prints 2 System.out.println(for(q in [x, y] where q < 3) q); // prints [ 2 ] System.out.println(Math.max(x, y)); // prints 4 System.out.println("abc".toUpperCase()); // prints ABC System.out.println(x); // prints 2