Operators

Expressions may be formed that combine variables and method results using arithmetic, assignment, bitwise, equality, logical and relational operators, as well as a few that do not fit with in these convenient categories.

Arithmetic operators include the usual add, subtract multiple, divide and reminder (modulas).

The assignment operators (=, +=, -=, *=, /=) include the simple assignment operator that we have already seen:

a = 1;

and also include convenience combinations of arithmetic and assignment in one operator, such as

a += 5; // Add 5 to a

Bitwise operators are rarely used in the Patchworks API, and are used to combine bit patterns in integral primitive variables. Search the web for “Java bitwise operators” for more information about these operators.

Relational operators include equal (==), not equal (!=), greater than (>), greater than or equal to (>=), less than (<) and less than or equal to (<=). Relational operators test the left and right hand values and return a Boolean value (true/false) having the result of the test.

a = 2;
if (a < 5)
   print("less than 5");

[Note]Object equality and comparisons

These relational operators test the values that are on either side of the operator. As we noted before, primitive values are stored directly in the variable so the above comparison will work as expected, but objects are stored as a reference to the object in heap memory. When you try to use the relational operators to compare objects, you will actually be comparing object references which in most cases is meaningless. For example, we could easily have two different String objects in two different locations in heap memory that both contain the value "John". If we compared these two references with an == test the result would be false because the references are different, when in fact the string values within the objects are identical. Similarly the > and < operators are only comparing the heap memory locations, not the object contents.

Instead, when comparing objects for equality we must use the equals() method that is implemented by all objects. The equals() method will perform the steps required to compare object content, which may need several comparisons for complex objects. For example:

firstName = "John";
if (firstName.equals("John"))
   print("They are the same");

If you want to do an ordering comparison (such as "less than" or "greater than"), then you will need to use the compareTo() method which is supported by classes that have a "natural ordering", such as numbers and strings. The compareTo() method compares one object to another of the same type, and returns an integer that is less than, greater than or equal to zero depending on whether the first object is less than, greater than or equal to the second object:

a = "q";
if (a.compareTo("m") > 0)
   print("The string '"+m+"' is higher in sorting order than 'm'");

The instanceof type comparison operator compares an object to the specified type. The operator returns true If the object or one of its super classes matches the specified type:

a = "John";
if (a instanceof String)
   print(a+" is a String type");

Logical operators combine Boolean values using ‘and’ (&&) or ‘or’ (||) tests. These operators are most often used to combine multiple relational test.

if (a >= 0 && a <= 5)
   print(“The value of a is between 0 and 5”);

The ternary operator is an expression element that is similar to an if-else statement. The operator contains three parts: the conditional test, the true value and the false value. The conditional test is evaluated and if true the true value is used as the result of the operator, otherwise the false value is used.

print("a is "+((a<5) ? "less than" : "greater than")+" five");

Ternary expression can reduce the number of lines required to test and use a value. They can also be difficult to read and benefit from the liberal use of parenthesis to clearly indicate the intent of the operation.

See Java documentation about operators or search the web for “Java operators” for details about the complete set of operators.