JavaFX Script for Java developers, Part 2

Comfortable arrays

Arrays in JavaFX are easy-to-use and act more like sequences. Here’s one:

var letters = ['A', 'B', 'D'];

Let’s print it,


Displaying an array in JavaFX

get its size,

println(sizeof letters);

Getting the text of an array in JavaFX

reverse it,

println(reverse letters);

insert an element at the end,

insert 'E' into letters;

insert more elements at the end,

insert ['F', 'G'] into letters;

and insert something at the middle.

insert 'C' after letters[1];

An array in JavaFX

To delete any element you may specify its value,

delete 'E' from letters;

or index.

delete letters[2];

Finally, you may delete all elements with

delete letters;

An empty array in JavaFX

Powerful selection

Here’s an array of various numbers.

var numbers = [78, 44, 21, 7, 90, 4, 18];

Let’s print those that are strictly smaller than 10,

println(numbers[n | n < 10]);

Note: | means where and n is an arbitrary variable name according to the developer’s taste.

Here’s how to select the elements between 20 and 50,

println(numbers[n | 20 <= n and n <= 50]);

select only the even elements,

println(numbers[n | n mod 2 == 0]);

a sublist,


and a tail list.


Arrays as ranges

Check this out.

var numbers = [1..10];

To ommit the last value, simply add a <.

var numbers = [1..<10];

Here are the multiples of 17 up to 100.

var multiples = [0..100][n | n mod 17 == 0];


var multiples = [0..100 step 17];


Such a prominent data structure should be accompanied by a set of utilities. The equivalent of java.util.Arrays is here javafx.util.Sequences.

It provides a rich toolset for searching, sorting, finding the minimum and maximum values. Of course, all this functionality is based on the natural order (Comparable) or a custom order (Comparator).

For example, we’re developing a game.

class Player {
    var name: String;
    var age: Integer;

It’s good to provide a proper toString() implementation.

class Player {
    var name: String;
    var age: Integer;
    override function toString(): String {
        return name;

Let’s define a natural order. For a player, his age makes sense.

class Player extends Comparable {
    var name: String;
    var age: Integer;
    override function toString(): String {
        return name;
    override function compareTo(other: Object): Integer {
        return age - (other as Player).age;

Here are a few of them…

var players = [
    Player {name: 'Madonna' age: 56},
    Player {name: 'Katie Perry' age: 24},
    Player {name: 'Jennifer Lopez'  age: 38}];

Let’s print them,


A sequence of singers, unsorted.

and sort them.


A sorted sequence.

The powerful selectors do apply here as well.

println(players[p | p.age > 30]);

Powerful selection on a sequence.

println(players[p |'M')]);

Powerful selection on a sequence, example 2.

Before moving on, let’s try to modify the Player class a bit.

class Player extends Comparable<Player> {
    var name: String;
    var age: Integer;
    override function compareTo(other: Player): Integer {
        return age - other.age;

Compilation fails, because JavaFX does not yet support generic parameters.

Enhanced for loop

The enhanced for loop of Java is here.

for (player in players) {

At a first glance, just the keyword in is introduced. But there’s more.

Make statistics out of the players’ ages :

var ages = for (player in players) player.age;

Quickly get a list of the players’ names :

var activePlayers = for (player in players);

Get their names in capital letters :

var listPlayers = for (player in players);

Actually you may get an array from any loop, if the last statement returns something.

var array = for (number in [1..100]) {
    // more statements

Even the following is valid.

var array = for (number in [1..100]) {
    // more statements

This feature is useful for fast graphics transformations.

var cubes = for (number in [1..100]) number * number * number;

As an addition to Java, protection is provided for the parameter; it’s value cannot be altered.

for (number in [1..10]) {
    // compilation fails
    number = number + 2;

Strings and formatting

Strings may be declared with single quotes as well. The following initializations are all valid.

var text1 = "Create ";
var text2 = 'richer ';
var text3 = new String("internet ");
var text4 = new String('applications.');

Here’s a complete sentence with some Java chaining,

var sentence = text1.concat(text2).concat(text3).concat(text4);

or a StringBuilder.

sentence = new StringBuilder(text1).append(text2).append(text3).append(text4).toString();

Fortunately, the plus operator for strings is well deprecated.

sentence = text1 + text2 + text3 + text4; // compilation fails

Even better, a new feature is here.

var number = 10;
println("A number: {number}");

What’s between the braces gets evaluated.

var number = 10;
println("It's square: {number * number}");

So these statements in Java,

int amount = 7;
String status = String.format("%d dollars left.", amount);
System.out.format("If only I had %d!\n", 1000 * amount);

in JavaFX would be:

var amount = 7;
var status = "{amount} dollars left.";
println("If only I had {1000 * amount}!");

More examples.

sentence = "{text1}{text2}{text3}{text4}";

String formatting in JavaFX, example 1

sentence = "Visualise {text2}{text3}{text4}";

String formatting in JavaFX, example 2

sentence = "Visualise {text2.toUpperCase()}{text3}{text4}";

String formatting in JavaFX, example 3

sentence = "Visualise {text2.toUpperCase()}{text3}{if (true) 'experiences.' else text4}";

String formatting in JavaFX, example 4

Wrap up

In JavaFX casting is provided by the “as” keyword. Also, “extends” is used for any IS-A relationship.

class Child extends Parent, Comparable {
    // implementation

Consider this small sequence.

var letters = ['A', 'B', 'D'];

JavaFX provides compile-time type safety for arrays. For example, this fragment will not compile successfully.

// assigning an Integer to a String
letters[0] = 100;
// inserting an Integer to a String[]
insert 100 into letters;

On the other hand, JavaFX gracefully handles some common unchecked exceptions. Consider this example:

// exceeding the bounds of an array
delete letters[100];
letters[100] = 'Z';
println("No exception is thrown!");

The last part of this series will focus on two brand-new features of JavaFX Script.

Thank you.

4 Responses to JavaFX Script for Java developers, Part 2

  1. finalboss says:

    Συγχαρητήρια για το blog! Μήπως μπορείς να φτιάξεις κανένα καλό mailbomber ή invisible keylogger να γουστάρουμε? Για παράδειγμα ένα άπλετ που όποιος το τρέχει να καταγράφονται όλα τα keystrokes του και να αποστέλλονται με email. Απ ότι βλέπω υπάρχουν ήδη κάποιες πρώτες αξιολογες προσπάθειες Keep up the good work!

  2. This is a great intro to JavaFX Sequences! Thanks!

  3. […] Introduction to the JavaFX language: part1 and part2 […]

  4. […] of people and I want to sort by age) the other day, found the following two links which helped: one and […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: