Operators are built-in functions for manipulating (usually primitive) values. In Java, we can define new methods, but we cannot define new operators.
Arithmetic Operators
Arithmetic operators are summarized in Figure A-18.
Operator |
Description |
Notes |
---|---|---|
+ |
addition |
chars can be added with Strings, this is the concatenation operator |
- |
subtraction |
can be applied to a single value (for example, -x) chars can be subtracted |
* |
multiplication |
|
/ |
division |
with ints, remainder is discarded |
% |
remainder |
behaves like modulo with positive numbers |
These generally behave as one would expect, but we must remember the following:
Assignment Operators
Assignment operators are used to set or change the value of a variable. They are summarized in Figure A-19.
Operator |
Description |
Notes |
---|---|---|
= |
assignment |
returns a value |
++ |
increment |
returns a value, but the value should not be used |
-- |
decrement |
returns a value, but the value should not be used |
+= |
increment by |
x += y is equivalent to x = x + y |
-= |
decrement by |
x -= y is equivalent to x = x - y |
*= |
multiply by |
x *= y is equivalent to x = x * y |
/= |
divide by |
x /= y is equivalent to x = x / y |
%= |
remainder by |
x %= y is equivalent to x = x % y |
Things to watch out for:
x = (y = (z = 3));
It is okay to omit the parentheses in this case:
x = y = z = 3;
A second consequence is that if we accidentally type
if (love = true) { ... }
when we mean to type
if (love == true) { ... }
then the test will always be passed, because love = true is an assignment statement that returns true. We can avoid this issue by using the equivalent code:
if (love) { ... }
y = x++;
Now x is 3, but what is y? There is an answer, but it's not immediately obvious whether it's 2 or 3. Worse, the answer is different for:
y = ++x;
Rather than ask anyone reading our programs to perform these mental gymnastics, we'll just avoid using the return value of a ++ or -- expression.
Comparison Operators
These operators, used to compare values, are summarized in Figure A-20.
Operator |
Description |
Notes |
---|---|---|
== |
equality |
use only with primitive values |
!= |
inequality |
use only with primitive values |
< |
less than |
|
<= |
less than or equal to |
|
> |
greater than |
|
>= |
greater than or equal to |
The only thing to watch out for here is that == and != should be used only to compare primitive values. If they are used to compare (for example) Strings, strange things can happen. This is explained in much more detail in Chapter 2.
Logical Operators
Figure A-21 shows some operators for manipulating boolean values.
Operator |
Description |
Notes |
---|---|---|
&& |
and |
short-circuited |
|| |
or |
short-circuited |
! |
not |
The && and || operators are short-circuited, meaning that Java stops as soon as it knows the answer. In addition to saving computation time, this is particularly useful for expressions such as:
(bears.length > 10) && (bears[10] == "hungry")
If the left side evaluates to false, the entire expression must evaluate to false, so Java does not bother evaluating the right side. Since the right side is not evaluated, it does not cause a problem if there is no such element as bears[10].
Perversely, if you accidentally type & instead of && (or | instead of ||), the Java compiler won't complainbut you'll get a non-short-circuited version.
Java has a few other operators, but they are fairly obscure. The only other ones we will use in this book are the bitwise operators discussed in Section 12.1.
Exercises
A.17 |
What is the value of 3 / 4? How about 3.0 / 4.0? |
A.18 |
What is the value of 123 % 20? |
A.19 |
Which assignment operators (Figure A-19) can be used to initialize a variable? |
A.20 |
Change line 15 of Figure A-11 to read: if (comparison = '<') { Why is this an error? Does the compiler catch it? |
A.21 |
Write a program which contains the erroneous expression args[-1] == args[0] but still compiles and runs. (Hint: Use a short-circuited logical operator.) |
Part I: Object-Oriented Programming
Encapsulation
Polymorphism
Inheritance
Part II: Linear Structures
Stacks and Queues
Array-Based Structures
Linked Structures
Part III: Algorithms
Analysis of Algorithms
Searching and Sorting
Recursion
Part IV: Trees and Sets
Trees
Sets
Part V: Advanced Topics
Advanced Linear Structures
Strings
Advanced Trees
Graphs
Memory Management
Out to the Disk
Part VI: Appendices
A. Review of Java
B. Unified Modeling Language
C. Summation Formulae
D. Further Reading
Index