Sun Java Solaris Communities My SDN Account Join SDN

Learning the JavaFX Script Programming Language - Tutorial Overview

Lesson 4: Data Types

   
« Previous 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Next »
 
By now you've learned about variables, functions, and objects. This lesson explores the built-in data types. The JavaFX Script programming language supports character string types, numeric types, boolean (true/false) types. It also supports time-based (duration) types, plus special types that represent void and null.

This lessons focuses on the basic information that most script writers will want to know. For a lower-level discussion of the type system, see Chapter 2. Types and Values of the JavaFX Script Programming Language Reference.
 
Contents
 
String
Number and Integer
Boolean
Duration
Void
Null
 
String

You have seen many String examples already, but let's take a closer look at this data type. A String can be declared using either single or double quotes:

var s1 = 'Hello';
var s2 = "Hello";
 

Single and double quotes are symmetrical: you can embed single quotes within double quotes, and double quotes within single quotes. There is no difference between single- and double-quoted strings.

You can also embed expressions inside a string using curly braces "{}":

def 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:

def answer = true;
var s = "The answer is {if (answer) "Yes" else "No"}"; // s = 'The answer is Yes'
 

The compiler will substitute the bold expression above with the string "Yes" if the value of answer is true or "No" otherwise.

To join (concatenate) multiple strings, use curly braces inside the quotes:

def one = "This example ";
def two = "joins two strings.";
def three = "{one}{two}";      // join string one and string two
println(three);                // 'This example joins two strings.'
 
Number and Integer

The Number and Integer types represent numerical data, although for most scripting tasks, you will usually just let the compiler infer the correct type:

def numOne = 1.0; // compiler will infer Number 
def numTwo = 1;   // compiler will infer Integer
 

You can, however, explicitly declare a variable's type:

def numOne : Number = 1.0;
def numTwo : Integer = 1;
 

The difference between these two types is that Number represents floating-point numbers, but Integer only represents integers. Use Number only when you absolutely need floating-point precision. In all other cases, Integer should be your first choice.


Note: As of SDK 1.1, the language also contains numeric types that align with those found in the Java programming language. The full list of numeric types is therefore: Byte, Short, Number, Integer, Long, Float, Double, and Character. However, the advice given above still holds true: most programmers will only need Integer (or Number) for the scripts that they write. If you are coming to this language from a Java programming language background and have a task that absolutely requires one of the other types, just know that these additional types are now available to your scripts.
Boolean

The Boolean type represents two values: true or false. Use this type when setting some application-specific internal state:

var isAsleep = true; 
 

or when evaluating a conditional expression:

if (isAsleep) {
     wakeUp();
}
 

If the expression inside the braces, "()", is true, the code inside the curly braces, "{}" is executed. For more information on conditional expressions, please see the Expressions lesson.

Duration

The Duration type represents a fixed unit of time (millisecond, second, minute, or hour.)

5ms; // 5 milliseconds
10s; // 10 seconds
30m; // 30 minutes
1h;  // 1 hour
 

Durations are notated with time literals — for example, 5m is a time literal representing five minutes. Time literals are most often used in animation (which you will learn about in the Creating Animated Objects lesson, part of the Building GUI Applications with JavaFX).

Void

Void is used to indicate that a function does not return any value:

function printMe() : Void {
     println("I don't return anything!");
}
 

This is equivalent to the following, which omits the function's return type:

function printMe() {
     println("I don't return anything!");
}
 

The JavaFX keyword Void begins with a capital V. If you are familiar with void in the Java Programming Language, you should take note of this.


Note: In JavaFX, everything is an expression. The return type of the second printMe function is also Void, as the compiler was able to infer its type. You will learn more about this in the Expressions lesson.
Null

Null is a special value used to indicate a missing normal value. Null is not the same as zero or an empty string, so comparing for null is not the same as comparing for zero or an empty string.

The null keyword allows comparisons to be made. It is common to see null used like this:

function checkArg(arg1: Address) {
     if(arg1 == null) {
          println("I received a null argument.");
     } else {
          println("The argument has a value.");
     }
}
 

This function accepts one argument, then performs a simple test to check if its value is null.

 
« Previous 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Next »
 
 
Rate This Article
 
Comments
Do you have comments about this article? We welcome your participation in our community. Please keep your comments civil and on point. You may optionally provide your email address to be notified of replies - your information is not used for any other purpose. By submitting a comment, you agree to these Terms of Use.