A.8. Operators

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.

Figure A-18. Arithmetic operators.

(This item is displayed on page 534 in the print version)

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.

Figure A-19. Assignment operators.

(This item is displayed on page 535 in the print version)

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:

Comparison Operators

These operators, used to compare values, are summarized in Figure A-20.

Figure A-20. Comparison operators.

(This item is displayed on page 536 in the print version)

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.

Figure A-21. Logical operators.

(This item is displayed on page 536 in the print version)

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.)

Категории