Expressions

Goals

Concepts

Lesson

An expression represents the result of some comparison or mathematical operation, such as adding two things (e.g. x + 2) or seeing if one number or variable is greater than another (e.g. x > y). Expressions can be made up of variables, literals, and/or symbols called operators. The result of an expression is a value of some type—normally the type of the values in the expression, but not always. Here are some examples of expressions:

Mathematical Operators

In your expressions most of the operators you'll use are those you are already familiar with in arithmetic, including +, -, *, and /. These operators for the most part have the precedence you are used to (e.g. multiplication takes precedence over addition), and you can override precedence by using parentheses ( and ). See the References section if you have a doubt. Of special note is the % operator, which is the remainder or modulus operator of division. You can also compare numbers using <, <=, ==, !=, >=, and >.

int sum = 1 + 80 + 92;  //173
int result = (2 + 70) / (3 * 6); //note operator precedence
double third = (double)result / 3; //note conversion

Equality Operators

You've already seen that Java uses = to indicate a variable assignment. Two equals signs together make up an expression to determine if two primitive values are identical, such as 4 + 5 == 9. The result of this expression is the boolean value true.

Logical Operators

When part or all of an expression evaluates to a boolean type, you can use && (logical AND), || (logical OR), and ! (logical NOT). The operator && takes precedence over ||, but in your own code use parentheses to keep from confusing others.

int result = (2 + 70) / (3 * 6); //note operator precedence
boolean isFour = result == 4;
boolean isFive = result == 5;
boolean isSmall = result < 10;
boolean isSmallButNotSix = isSmall && result != 6;
boolean isSmallButNotFive = isSmall && !isFive;

You may notice that there are certain rules that the logical operators always follow used in expressions. For example, for expressions involving || if either side is true then the outcome will be true. In an expression involving && both sides of the expression must evaluate to true or the the result will be false.

The rules of Boolean expressions allow us to create a truth table containing all possible outcomes involving each logical operator.

Logical Truth Table
Input Values AND OR XOR NOT
boolean foo boolean bar foo && bar foo || bar foo ^ bar !foo
false false false false false true
false true false true true true
true false false true true false
true true true true false false

Assignment

You've already used the assignment operator =, but there are some other operators that do assignment and a little bit more. These were inherited from C/C++, and while cool if you're trying to be clever, most of the time you should not be clever but smart. Don't overuse these. One exception is within loops, where it's expected to see an increment operator (i++). Here are some examples of assignment operators. See the References section for a complete list.

Example Same As
a += b a = a + b
i++ i = i + 1
int i = 5;
int before = i++;  //i==6, before==5
int after = ++i;  //i==7, after=7

Ternary Operator

One last operator is especially cool, acting like a test. The expression (a ? b : c) (parentheses added for clarity) means: “If a is true, then yield the value b; otherwise, yield the value c.”

int admissionPrice = attendeeNumber <= 50 ? 10 : 20;
The admission price for the first 50 attendees will be $10, but for the rest the price will be $20.
Example of a ternary operator being used for determining the admission to a show based upon the order of attendees.
Here are some more examples
int a = 5;
System.out.println(a * 3 == 15 ? "foo" : "bar"); //prints "foo"
System.out.println(a < 3 ? "foo" : "bar"); //prints "bar"

Review

Summary

Operator Precedence
Operators Precedence
additive + -
assignment = += -= *= /= %= &= ^= <<= >>= >>>=
bitwise exclusive OR ^
equality == !=
logical AND &&
logical OR ||
multiplicative * / %
postfix expr++ expr--
relational < > <= >= instanceof
shift << >> >>>
ternary ? :
unary ++expr --expr +expr -expr ~ !

Gotchas

In the Real World

Think About It

Self Evaluation

Task

Create a program that makes the following determinations using expressions:

Create a compressed archive of your entire project directory and send it to your teacher as you have in previous lessons.

See Also

References

Acknowledgments