JAVA;
Operators and Expressions
Operators

The countChars method uses several operators including =, !=, ++, and +,
which are highlighted in this listing:

import java.io.*
public class Count {
public static void countChars(Reader in) throws IOException
{
int count = 0

while (in.read() != -1){
count++
System.out.println("Counted " + count + " chars.")
}
// ... main method omitted ...
}

Operators perform some function on either one or two operands or three operands. Operators that require one operand are called unary operators. For example, ++ is a unary operator that increments the value of its operand by 1

Operators that require two operands are binary operators. For example, = is a binary operator that assigns the value from its right-hand operand to its left-hand operand. And finally tertiary operators are those that require three operands. The Java language has one tertiary operator, ?:, which is a short-hand if-else statement.

Java's unary operators can use either prefix or postfix notation. Prefix notation means that the operator appears before its operand:

operator op

Postfix notation means that the operator appears after its operand:

op operator

All of Java's binary operators use infix notation, which means that the operator appears between its operands:

op1 operator op2

Java's only tertiary operator is also infix; each component of the operator appears between operands:

expr ? op1 : op2

In addition to performing the operation, an operator also returns a value. The value and its type depends on the operator and the type of its operands. For example, the arithmetic operators, which perform basic arithmetic operations such as addition and subtraction, return numbers-the result of the arithmetic operation. The data type returned by the arithmetic operators depends on the type of its operands: If you add two integers, you get an integer back. An operation is said to evaluate to its result.

It's useful to divide Java's operators into these categories: arithmetic, relational and conditional, bitwise and logical, and assignment. These are discussed in the following sections.

Arithmetic Operators

The Java language supports various arithmetic operators for all
floating-point and integer numbers. These include + (addition), -
(subtraction), * (multiplication), / (division), and % (modulo). For
example, you can use this Java code to add two numbers:

addThis + toThis

Or you can use the following Java code to compute the remainder that
results from dividing divideThis by byThis:

divideThis % byThis

This table summarizes Java's binary arithmetic operations:

Operator
Use
Description
+ op1+op2 Adds
- op1-op2 Subtracts
* op1*op2 Multiplies
/ op1/op2 Divides
% op1%op2 Computes remainder


Note: The Java language extends the definition of the + operator
to include string concatenation. The countChars method uses
+ to concatenate "Counted ", the value of count, and "
chars.", as shown here:

System.out.println("Counted " + count + " chars.");

There also are two short cut arithmetic operators, ++ which
increments its operand by 1, and -- which decrements its
operand by 1. The countChars method uses ++ to increment
the count variable each time it reads a character from the input
source with this statement:

count++

Note that the ++ operator appears after its operand in this
example. This is the postfix version of the operator. ++ also has a
prefix version in which ++ appears before its operand. Both the
prefix and postfix versions of this operator increment the operand
by 1. So why are there two different versions? Because each
version evaluates a different value: op++ evaluates to the value of
the operand before the increment operation, and ++op evaluates
the value of the operand after the increment operation.

Relational and Conditional Operators

A relational operator compares two values and determines the
relationship between them. For example, != returns true if the
two operands are unequal. The countChars method uses != to
determine whether the value returned by in.read is not equal
to -1. This table summarizes Java's relational operators:

Operator
Use
Return true if:
">" op1 >op2 op1 is greater than op2
>= op1>=op2 op1 is more/equal to op2
< op1<op2 op1 is less than op2
<= op1<=op2 op1 is less/equal to op2
== op1==op2 op1 is equal to op2
!= op1!=op2 op1 is not equal to op2




Relational operators often are used with the conditional operators to construct more complex decision-making expressions. One such operator is &&, which performs the boolean and operation. For example, you can use two different relational operators along with && to determine if both relationships are true. The following line of code uses this technique to determine if an array index is between two boundaries. It determines if the index is both greater than 0 and less than NUM_ENTRIES (which is a previously defined constant value)

0 < index && index < NUM_ENTRIES

Note that in some instances, the second operand to a conditional operator may not be evaluated. Consider this statement:

((count > NUM_ENTRIES) && (in.read() != -1))

If count is less than NUM_ENTRIES, the left-hand operand for && evaluates to false. The && operator will return true only if both operands are true. So in this situation, the return value of && can be determined without evaluating the right-hand operand. In such a case, Java will not evaluate the right-hand operand. Thus,in.read won't get called and a character will not be read from standard input.

Another Binary Conditional operator is the || symbol if both of its operands are boolean, This operator always evalutes both of its operands and returns false if they are both false.

Java supports five binary conditional operators, shown in the following table:

Operator Use Returns True if-
&& op1 && op2 If op1 and op2 are true, conditionally evaluates op2
|| op1 || op2 either op1 or op2 is true, conditionally evaluates op2
! !op1 op1 is false
| op1 | op2 either op1 or op2 is true, doesn't conditionally evaluate op2
op1 & op2 op1 & op2 op1 and op2 are true, doesn't conditionally evaluate op2

Assignment Operators

You use the basic assignment operator, =, to assign one value to another. The countChars method uses = to initialize count with this statement:

int count = 0;

Java also provides several short cut assignment operators that allow you to perform an arithmetic, logical, or bitwise operation and an assignment operation all with one operator. Suppose you wanted to add a number to a variable and assign the result back into the variable, like this:

i = i + 2;

You can shorten this statement using the short cut operator +=.

i += 2;

The two previous lines of code are equivalent.

This table lists the shortcut assignment operators and their lengthy equivalents:

Operator Use Equivalent to
+= op1 += op2 op1 = op1 + op2
-= op1 -= op2 op1 = op1 - op2
*= op1 *= op2 op1 = op1 * op2
/= op1 /= op2 op1 = op1 / op2
%= op1 %= op2 op1 = op1 % op2
^= op1 ^= op2 op1 = op1 ^ op2

Go on to Page 4

This page was created by Bob Perini
For CS 763(Web Seminar) at SUNY Buffalo