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 3 of 5

Functions

JavaFX Script's use of functions for specifying reusable blocks of code is also interesting. Although functions must have names (unless they're anonymous), return types don't need to be specified. If this type isn't present, and if no value is returned, the return type defaults to Void. To explictly identify this return type, specify it after the parameter list, as in function output (): Void {}.

A function's body is specified via a block. The last item of executable code within a block doesn't require a semicolon. In the previous script, the last item is a System.out.println() method call, which demonstrates yet another interesting feature: one or more expressions can be embedded within a string literal by surrounding each expression with brace characters ({}).

Finally, the script reveals that you can declare variables and functions outside of a class context, which is in sharp contrast to Java, where field variables and methods must be declared within classes. A variable declared in this manner is accessible to a function's code, although it can be hidden by declaring a same-named local variable within the function.

Running the script

Just one script has told us quite a bit about how JavaFX Script differs from the Java language, and also leverages it. After entering the script in Listing 1, compile and run the code by clicking the green triangle (Run Main Project) button on the toolbar (or press F6). As revealed in Figure 1, the output window displays the compilation results, and also all output sent to the standard output via System.out.println() method calls.

If compilation succeeds, the screen displays 'JavaFX' on one line, followed by 'name2 = JavaFX Script'.

Figure 1. Results of a successful compilation (click to enlarge)

Now that we've reviewed some language fundamentals, we can begin to look at expressions, which are the building blocks of JavaFX programs. We'll study some examples of simple expressions and review the operators for combining simple expressions into more complex ones. After that, I'll show you how JavaFX Script uses expressions to handle familiar Java constructs such as conditionals, loops, ranges, and exception handling, as well as expressions based on function pointers.

An expression language

According to Sun's JavaFX Script Programming Language Reference (currently in draft), JavaFX Script is an expression language. Apart from package and import statements, and variable declarations, everything else is an expression with zero or more inputs, and zero or one output. The script in Listing 2 demonstrates some simple expressions:

Listing 2. Simple expressions in JavaFX Script

var name = "JavaFX";
java.lang.System.out.println (name+" Script"); // Output: JavaFX Script
java.lang.System.out.println (name.equalsIgnoreCase ("JAVAFX")); // Output: true
java.lang.System.out.println (8 mod 3); // Output: 2
var circleArea = java.lang.Math.PI*10*10;
java.lang.System.out.println (circleArea); // Output: 314.1592653589793
var area = circleArea as Integer; // cast from Number type to Integer type
java.lang.System.out.println (area); // Output: 314
// The following expression demonstrates an alternative Number-to-Integer cast.
java.lang.System.out.println (circleArea.intValue ()); // Output: 314
java.lang.System.out.println (37.5.intValue ()); // Output: 37
// The following method call outputs: Hex equivalent of 1234: 04D2 
java.lang.System.out.println ("Hex equivalent of 1234: {%04X 1234}");
var millis = java.lang.System.currentTimeMillis ();
// The following method call outputs a date/time combination such as:
// Current date/time: Fri Sep 10 20:53:09 CDT 2008
java.lang.System.out.println ("Current date/time: {%tc millis}")

You might be curious about the {%04X 1234} expression in "Hex equivalent of 1234: {%04X 1234}", and the {%tc millis} expression in "Current date/time: {%tc millis}". These expressions reveal a language convenience for converting numbers and dates into formatted character sequences, via a java.util.Formatter formatting prefix followed by the item being formatted.

  • Print
  • Feedback

Resources

 

More from JavaWorld