All About Operators

Statements can use mathematical expressions by employing the operators +, -, *, /, and %. You use these operators to crunch numbers throughout your Java programs.

An addition expression in Java uses the + operator, as in these statements:

double weight = 205;
weight = weight + 10;

The second statement uses the + operator to set the weight variable equal to its current value plus 10. A subtraction expression uses the - operator:

weight = weight - 15;

This expression sets the weight variable equal to its current value minus 15.

A division expression uses the / sign:

weight = weight / 3;

This sets the weight variable to its current value divided by 3.

To find a remainder from a division expression, use the % operator (also called the modulo operator). The following statement finds the remainder of 245 divided by 3:

int remainder = 245 % 3;

A multiplication expression uses the * sign. Here’s a statement that employs a multiplication expression as part of a more complicated statement:

int total = 500 + (score * 12);

The score * 12 part of the expression multiplies score by 12. The full statement multiples score by 12 and adds 500 to the result. If score equals 20, the result is that total equals 740: 500 + (20 * 12).

Incrementing and Decrementing a Variable

A common task in programs is changing the value of a variable by one. You can increase the value by one, which is called incrementing the variable, or decrease the value by one, which is decrementing the variable. There are operators to accomplish both of these tasks.

To increment the value of a variable by one, use the ++ operator, as in the following statement:

x++;

This statement adds one to the value stored in the x variable.

To decrement the value of a variable by one, use the -- operator:

y--;

This statement reduces y by one.

You also can put the increment and decrement operators in front of the variable name, as in the following statements:

++x;
--y;


Note

Confused yet? This is easier than it sounds, if you think back to elementary school when you learned about prefixes. Just as a prefix such as “sub-” or “un-” goes at the start of a word, a prefix operator goes at the start of a variable name. A postfix operator goes at the end.


Putting the operator in front of the variable name is called prefixing, and putting it after the name is called postfixing.

The difference between prefixed and postfixed operators becomes important when you use the increment and decrement operators inside an expression.

Consider the following statements:

int x = 3;
int answer = x++ * 10;

What does the answer variable equal after these statements are handled? You might expect it to equal 40—which would be true if 3 was incremented by 1, which equals 4, and then 4 was multiplied by 10.

However, answer ends up with the value 30 because the postfixed operator was used instead of the prefixed operator.

When a postfixed operator is used on a variable inside an expression, the variable’s value doesn’t change until after the expression has been completely evaluated. The statement int answer = x++ * 10 does the same thing in the same order, as the following two statements:

int answer = x * 10;
x++;

The opposite is true of prefixed operators. If they are used on a variable inside an expression, the variable’s value changes before the expression is evaluated.

Consider the following statements:

int x = 3;
int answer = ++x * 10;

This does result in the answer variable being equal to 40. The prefixed operator causes the value of the x variable to be changed before the expression is evaluated. The statement int answer = ++x * 10 does the same thing in order, as these statements:

x++;
int answer = x * 10;

It’s easy to become exasperated with the ++ and operators because they’re not as straightforward as many of the concepts you encounter in this book.


Note

Back in Hour 1, “Becoming a Programmer,” the name of the C++ programming language was described as a joke you’d understand later. Now that you’ve been introduced to the increment operator ++, you have all the information you need to figure out why C++ has two plus signs in its name instead of just one. Because C++ adds new features and functionality to the C programming language, it can be considered an incremental increase to C—hence the name C++.

After you work through all 24 hours of this book, you too will be able to tell jokes like this that are incomprehensible to more than 99 percent of the world’s population.


I hope I’m not breaking some unwritten code of Java programmers by telling you this, but you don’t need to use the increment and decrement operators in your own programs. You can achieve the same results by using the + and operators like this:

x = x + 1;
y = y - 1;

Incrementing and decrementing are useful shortcuts, but taking the longer route in an expression is fine, too.

Operator Precedence

When you are using an expression with more than one operator, you need to know what order the computer uses as it works out the expression. Consider the following statements:

int y = 10;
x = y * 3 + 5;

Unless you know what order the computer uses when working out the math in these statements, you cannot be sure what the x variable will be set to. It could be set to either 35 or 80, depending on whether y * 3 is evaluated first or 3 + 5 is evaluated first.

The following order is used when working out an expression:

1. Incrementing and decrementing take place first.

2. Multiplication, division, and modulus division occur next.

3. Addition and subtraction follow.

4. Comparisons take place next.

5. The equal sign (=) is used to set a variable’s value.

Because multiplication takes place before addition, you can revisit the previous example and come up with the answer: y is multiplied by 3 first, which equals 30, and then 5 is added. The x variable is set to 35.

Comparisons are discussed during Hour 7. The rest has been described during this hour, so you should be able to figure out the result of the following statements:

int x = 5;
int number = x++ * 6 + 4 * 10 / 2;

These statements set the number variable equal to 50.

How does the computer come up with this total? First, the increment operator is handled, and x++ sets the value of the x variable to 6. However, make note that the ++ operator is postfixed after x in the expression. This means that the expression is evaluated with the original value of x.

Because the original value of x is used before the variable is incremented, the expression becomes the following:

int number = 5 * 6 + 4 * 10 / 2;

Now, multiplication and division are handled from left to right. First, 5 is multiplied by 6, 4 is multiplied by 10, and that result is divided by 2 (4 * 10 / 2). The expression becomes the following:

int number = 30 + 20;

This expression results in the number variable being set to 50.

If you want an expression to be evaluated in a different order, you can use parentheses to group parts of an expression that should be handled first. For example, the expression x = 5 * 3 + 2; would normally cause x to equal 17 because multiplication is handled before addition. However, look at a modified form of that expression:

x = 5 * (3 + 2);

In this case, the expression within the parentheses is handled first, so the result equals 25. You can use parentheses as often as needed in a statement.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.62.168