Expressions and Operators...

This document was written by CS 290W TA Joshua Kay and was last modified

Let's check out some expressions that Java allows. When you combine a set of variables with a set of operators, then you create an expression which the computer will calculate. Ex:
perimeter = 10 + 10;
area = width * height ;
courseName = "cs" + "290w";
The last one is a special case of strings - in that the plus sign can actually concatenate two strings. (The result is the string "cs290w".) But we will deal with that more in a little while. Let's look at a table of basic operators:

SymbolActionExample
+addition3+2 or "con"+"cat"
-subtraction12-3 OR -3
*multiplicationlength * width
/divisionmiles/gallons
%modulus11%4 (see below)
&bitwise andnum&mask
|bitwise orthis|that
^bitwise xortall^short
~bitwise complementmask=~item
<<left shiftvar<<
>>right shift>>var
<<<left shift with zero fillarg<<<howMuch
>>>right shift with zero fillarg>>>howMuch

Now, for the purpose of CS 290W, we will only be using +, -, *, /, and %. The first four are self-explanatory. You might not know the modulus operator, though.

Basically, the modulus returns the remainder. Here are some examples

11/4= 2 remainder 3 	so 11%4=3
20/5=4 no remainder	so 20%5=0
100/51=1 remainder 49	so 100%51=49
11/5 = 2 remainder 1	so 11%5=1
Okay, let's move on. Notice in the table above that for addition, it says "con"+"cat"? This is called String concatenation. Which means adding one string to the end of another. Example
"CS" + "290w" = "CS290w"
"Pur" + "due" = "Purdue"
Now there is another case of string concatenation, which we have already seen. Look at this...
int x=12;
g.drawString("There are " + x + " months in a year",5,5);
This will concatenate the number 12 onto the existing String, and then add on the end of the string. Pretty simple, but a very powerful asset of Java.

Now, we just looked at basic operators, which leaves us with the boolean operators. Now, as in JavaScript, boolean refers to the values true and false. Unlike many other Programming languages, you cannot substitute the values 1 or 0 for true and false. Let's look at them.

SymbolActionSample Expression
= =equal to(a= =b)
!=not equal to(a!=b)
<less than(a<b)
>greater than(a>b)
<=less than or equal to(a<=b)
>=greater than or equal to(a>=b)
&&and((a>b)&&(a<c))
||or((a= =b)||(a= =c))
!not!(x= =y)
^xor (exclusive or)((a= =b)^(a= =c))

Okay, note quickly that all but one of these take two operands. Only the ("!") operator takes one argument. All of the operators return a single boolean value, either true or false.

We already discussed the ++ operator, let's look at it again. We know that it is a shortcut, correct? But what Exactly is it doing?

x++ says to "take the value of x, and then increment it by one." What does this mean? Well, let's look at an example

int x=5;
g.drawString("x= "+ (x++),10,20);
What will this print out?

Well, this will print out 5. It takes the value of x, which is five, and then increments it by one. What if we said ++x? Would that do anything? Let's see...

int x=5;
g.drawString("x= "+ (++x),10,20);
What will this print out?

This printed out 6! We just saw it! Why? Well, x++ (postfix increment operator) says to do everything in the statement and afterwards increment x by one. ++x (prefix increment operator) says to increment x by one and then do everything in the statement. So it's a little different... Watch this - it's a common source of errors among beginners.

There are some other operators, that mix assignment with more complicated arithmetic. If you want to increase x by a larger value, say 5, then you could write x+=5 - this is the same as saying x=x+5. Let's look at some examples...

Operator UseEquivalentExample
x+=yx=x+yx+=2 --> x=x+2
x-=yx=x-yx-=2 --> x=x-2
x*=yx=x*yx*=2 --> x=x*2
x/=yx=x/yx/=2 --> x=x/2
x%=yx=x%yx%=2 --> x=x%2