Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
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
Page 2 of 5
In this JavaWorld podcast, Andrew Glover interviews Param Singh and JavaFX Architect John Burkey for a developer's perspective on what you'll be able to do with the newly released JavaFX 1.0.
Chris Oliver's brainchild, F3, is the basis of JavaFX Script, which Sun announced at the JavaOne conference in May 2007. We'll spend a lot of time in this article looking at how declarative syntax manifests in JavaFX Script. At the end of the article I'll show you some examples of the language's sophisticated data binding functionality. For now, though, just consider how these two features set JavaFX Script apart from many other languages:
Another nice feature of F3 was declarative animation, which is also known as keyframe animation in JavaFX Script. Although declarative animation uses some syntactic sugar for ease of use, this feature is mostly API-based, so I won't discuss it until Part 3.
In the next section I'll introduce some of the fundamentals of JavaFX Script. As in the previous article in this series, you'll learn by doing, so you should start up your NetBeans IDE now. (See "Jump into JavaFX, Part 1" to get started with NetBeans 6.1 with JavaFX.)
Start NetBeans and use the New Project wizard to introduce a new LanguageDemo project with languagedemo.Main as the main file. As we explore JavaFX Script, we'll insert a series of scripts into Main.fx, replacing the previous script with a new one.
The first thing you'll do is replace the skeletal Main.fx's "// place your code here" line with the following script -- as you enter this script, you'll probably encounter some of the IDE's editor features,
including syntax highlighting, code completion, and small red octagon-shaped icons with exclamation marks that signify potential
errors until the code is completely entered:
import java.lang.System;
var name1: String = "JavaFX";
var name2 = "JavaFX Script";
output ();
function output ()
{
System.out.println (name1); // Output: JavaFX
System.out.println ("name2 = {name2}") // Output: name2 = JavaFX Script
}
This script reveals a few interesting things about JavaFX Script. You'll notice right away that JavaFX Script supports the
same single-line comment style as Java. It also supports the same multi-line and documentation comment styles. More importantly,
you can include Java method calls in JavaFX Script code, but you'll want to import the appropriate Java packages to save keystrokes.
For example, because JavaFX Script doesn't automatically import java.lang, you'll need to import this package, or prepend java.lang to each System.out.println() method call.
Now consider what else Listing 1 tells us about JavaFX Script.
Variable declaration in JavaFX Script begins with the var keyword, continues with a valid name, is optionally followed by a type specification, and ends with an optional initializer
for assigning an initial value to the variable. If a type isn't specified, the type is inferred from the initializer -- the
type is unknown if there's no type and no initializer.
The name must follow Java's rules for what constitutes a valid identifier (it must not start with a digit, for example). Furthermore,
the name must not be a reserved identifier (a literal such as null, true, or false; or a keyword used to name an operator, a statement-oriented expression, or something else).
JavaFX Script reserves 67 (and probably additional) identifiers:
abstract, after, and, as, assert, at, attribute, before, bind, bound, break, catch, class, continue, delete, else, exclusive, extends, false, finally, first, for, from, function, if, import, in, indexof, init, insert, instanceof, into, inverse, last, lazy, let, new, not, null, on, or, override, package, postinit, private, protected, public, readonly, replace, return, reverse, sizeof, static, step, super, then, this, throw, trigger, true, try, tween, typeof, var, where, while, and with.
If you need to use a reserved identifier to name a variable or function (although you really shouldn't), or if you need to
invoke an external Java method whose name matches a reserved identifier, you'll have to surround the reserved identifier with
doubled angle brackets. Example: var <<var>> = 10; System.out.println (<<var>>).
Each variable has a type, whether or not the type is explicitly specified. This type can be a Java type, a JavaFX user-defined
type, or one of the String, Number (double-precision float), Integer, and Boolean fundamental types -- you might argue that Void is also a fundamental type. These types are fundamental because they don't need to be imported.
The fact that the fundamental types are Java classes means that you can invoke their Java methods via variables or (except
for Integer) values. For example, you can specify "abc".length (), 37.3.parseDouble ("23"), var i: Integer = i.parseInt ("23"); (but not 26.parseInt ("23")), and true.toString ().
If a variable isn't explicitly initialized, its type determines its default value: empty string for String, 0.0 for Number, 0 for Integer, false for Boolean, and null for reference types other than javafx.lang.Duration (which is 0.0 milliseconds). If there's no type, the variable's value defaults to null.
If the initializer is a literal, it must be compatible with the type (if present). Valid literals include sequences of characters between single quotes or
double quotes (string literals), numeric values with decimal points (number literals), numeric values without decimal points
(integer literals), true and false (Boolean literals), null, object literals, and time literals.