|
Operators are special symbols that perform specific operations on one or two operands, and then return a result. The JavaFX Script programming language provides assignment operators, arithmetic operators, unary operators, equality and relational operators, conditional operators, and a type comparison operator. For a formal description of the supported operators, see Tables 6.2 - 6.8 in the JavaFX Script Programming Language Reference.
|
Contents
The assignment operator "=" is one of the most commonly used operators that you will encounter. Use it to assign the value on its right to the operand on its left:
result = num1 + num2;
days = ["Mon","Tue","Wed","Thu","Fri"];
|
You have already used this operator in many of the previous lessons.
The arithmetic operators make it possible to perform addition, subtraction, multiplication, and division. The mod operator divides one operand by another and returns the remainder as its result.
+ (additive operator)
- (subtraction operator)
* (multiplication operator)
/ (division operator)
mod (remainder operator)
|
The following script provides some examples:
var result = 1 + 2; // result is now 3
println(result);
result = result - 1; // result is now 2
println(result);
result = result * 2; // result is now 4
println(result);
result = result / 2; // result is now 2
println(result);
result = result + 8; // result is now 10
println(result);
result = result mod 7; // result is now 3
println(result);
|
You can also combine the arithmetic operators with the assignment operator to
create compound assignments. For example, result += 1; and result = result+1; both increment the value of result by 1.
var result = 0;
result += 1;
println(result); // result is now 1
result -= 1;
println(result); // result is now 0
result = 2;
result *= 5; // result is now 10
println(result);
result /= 2; // result is now 5
println(result);
|
The only arithmetic operator that cannot be used in this manner is mod. If, for example, you want to divide result by 2, then assign the remainder back to itself, you would need to write: result = result mod 2;
Most operators require two operands; the unary operators use just one operand and perform operations such as incrementing/decrementing a value by one, negating a number, or inverting the value of a boolean.
- Unary minus operator; negates a number
++ Increment operator; increments a value by 1
-- Decrement operator; decrements a value by 1
not Logical complement operator; inverts the value of a boolean
|
The following script tests the unary operators:
var result = 1; // result is now 1
result--; // result is now 0
println(result);
result++; // result is now 1
println(result);
result = -result; // result is now -1
println(result);
var success = false;
println(success); // false
println(not success); // true
|
The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code result++; and ++result; will both end in result being incremented by one. The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value. (You can remember which is which by ++result does the increment and then gets the value, while result++ gets the value and then does the increment.) If you are just performing a simple increment/decrement, it doesn't really matter which version you choose. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.
The following script illustrates this distinction:
var result = 3;
result++;
println(result); // result is now 4
++result;
println(result); // result is now 5
println(++result); // result is now 6
println(result++); // this still prints 6!
println(result); // but the result is now 7
|
The equality and relational operators determine if one operand is greater than, less than, equal to, or not equal to another operand.
== equal to
!= not equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to
|
The following script tests these operators:
def num1 = 1;
def num2 = 2;
println(num1 == num2); // prints false
println(num1 != num2); // prints true
println(num1 > num2); // prints false
println(num1 >= num2); // prints false
println(num1 < num2); // prints true
println(num1 <= num2); // prints true
|
The conditional and and conditional or operators perform conditional operations on two boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed: For example, for an and operation, if the result of the first expression is false, the second expression will not be evaluated. For an or operation, if the result of the first expression is true the second expression will not be evaluated.
The following script demonstrates the use of these operators by defining username and password variables, then printing out matches of various conditions:
def username = "foo";
def password = "bar";
if ((username == "foo") and (password == "bar")) {
println("Test 1: username AND password are correct");
}
if ((username == "") and (password == "bar")) {
println("Test 2: username AND password is correct");
}
if ((username == "foo") or (password == "bar")) {
println("Test 3: username OR password is correct");
}
if ((username == "") or (password == "bar")) {
println("Test 4: username OR password is correct");
}
|
The output is:
Test 1: username AND password are correct
Test 3: username OR password is correct
Test 4: username OR password is correct
|
The instanceof operator compares an object to a specified type. You can use it to determine if an object is an instance of a particular class:
def str1="Hello";
println(str1 instanceof String); // prints true
def num = 1031;
println(num instanceof Integer); // prints true
|
You will find this operator to be useful as you learn more about classes and inheritance in the last lesson of this tutorial.
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.
|