As discussed in Chapter 1, the var
keyword is used to introduce a new variable into your program. A variable's type may be specified in its declaration, but doing so is optional. If a variable's type is omitted from the declaration, it may be inferred from its use.
A variable declaration takes the form:
var variableName : type = initializer;
Examples:
var num = 1; // inferred type var num : Number = 2; var firstName = "John"; // inferred type var lastName : String = "Doe";
Variable naming conventions are the same as found in the Java programming language. Classes should capitalize the first letter of each word (MyClass
); function names should begin in lowercase but capitalize the first letter of each subsequent word (myFunctionName
); constants should appear in all uppercase with words separated by the underscore character (MY_CONSTANT
.)
Any sequence of characters (including whitespace) contained in french quotes <<>> is treated as an identifier. This makes it possible to use JavaFX™ Script programming language keywords (or other normally illegal identifiers) as class, variable, function, or attribute names.
Example:
var <<delete>> = 100;
This also makes it possible to invoke methods (written in the Java programming language) whose names are the same as JavaFX Script programming language keywords.
import javax.swing.JTextArea; var textArea = new JTextArea(); textArea.<<insert>>("Hello", 0);
The lifetime of a variable is at least the lifetime of the containing block. It can be longer if the variable is captured by a closure. Top-level variables are visible inside functions and classes. They act like private static fields of the module class.
The JavaFX Script programming language does not use the term primitive types; instead, the language defines five basic data types, which are always available to your application code.
The five basic data types map to the Java programming language as follows:
JavaFX | Java |
---|---|
String
|
java.lang.String
|
Boolean
|
java.lang.Boolean
|
Number |
java.lang.Number |
Integer |
byte,short,int,long,BigInteger
|
Duration |
N/A
|
The first four data types will already be familiar to most developers
since they are frequently used in the Java programming language.
The Duration
type, however,
is a new specific to the JavaFX Script programming language. The javafx.lang.Duration
class represents a unit of time
(millisecond, second, minute, or hour.) Because time is so integral to
animation, the language also supports time literals,
a shorthand for instantiating the Duration
class.
5ms; // 5 milliseconds 10s; // 10 seconds 30m; // 30 minutes 1h; // 1 hour
For the integral types, coercions are automatically performed when passing arguments or returning values to/from methods written in the Java programming language. In addition, implicit truncating coercions are performed when converting Number
s to Integer
s.
For character strings, it is possible to specify a string literal using either single or double quotes:
var s1 = 'Hello'; var s2 = "Hello";
The difference is that with the latter, it is possible to embed
expressions within curly brackets ({}
):
var name = 'Joe'; var s = "Hello {name}"; // s = 'Hello Joe'
When using curly brackets, the compiler infers a type for the embedded expression that it can coerce to a String
.
The embedded expression can itself contain quoted strings, which, in turn, may contain further embedded expressions:
var answer = true; var s = "The answer is {if (answer) "Yes" else "No"}"; // s = 'The answer is Yes'
Unlike the Java programming language, a JavaFX Script programming language double-quoted String literal can contain newlines:
It is also possible to control how numbers and dates are converted to character
strings by providing an additional formatting prefix in a string expression. Such a
prefix follows the specification of java.util.Formatter
:
import java.util.Date; var hexStr = "hex of 13 is 0x{%02X 13}"; // hexStr = "hex of 13 is 0x0D" var date = new Date(107, 10, 11); var dateStr = "{%tc date}" // dateStr = "Sun Nov 11 00:00:00 PST 2007"