As discussed in Chapter 1, the var
keyword is used to introduce a new variable into your program.
A variable's type can be specified in its declaration, but doing so is optional. If a variable's type is omitted from the declaration,
the type can be inferred from its use.
A variable declaration takes the following 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 identical to the conventions 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 white space) contained in double angle brackets <<>> is treated as an identifier. This enables the use of 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.
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 Script Programming Language | Java Programming Language |
---|---|
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 should already be familiar to most developers
because 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.) 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 or from methods written in the Java programming language.
For character strings, you can specify a string literal using either single or double quotes:
var s1 = 'Hello'; var s2 = "Hello";
The difference is that with the latter, you can embed
expressions within curly braces ({}
):
var name = 'Joe'; var s = "Hello {name}"; // s = 'Hello Joe'
The embedded expression can itself contain quoted strings, which, in turn, can 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.
You also can control how numbers and dates are converted to character
strings by providing an additional formatting prefix in a string expression.
This additional formatting 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"