Expressions and Operators
Introduction.
Without really thinking about it we have started forming what are called
mathematical expressions. This
is done in ways that are so similar to what is done in other computer
languages there is almost no need mention them. The same will
largely be true of the operators that
are used to build these mathematical expressions. The following table contains a list of the basic arithmetic operators. Even within this range of types of data one needs to consider issues about the precision of the representation within the computer. Things that are true or false require nowhere near as much memory to store as the numbers associated with space travel. But this motivates the classification of data into what are usually called data types. In Java there are some standard data types listed in the following tables. we also present some information about the ranges of representation and the amount of memory that is used. First we list what are called the primitive data types. Then we will present some derived data types that are worked with in their own classes. |
Fundamental Arithmetic Operators | ||
Operator |
Meaning |
Example |
+ | addition | 3 + 4 = 7 |
- | subtraction | 3 - 4 = -1 |
* | multiplication | 3 * 4 = 12 |
/ | division | 12/5 = 2.4 (depending on data type) |
% | modulus (remainder operator) |
12 % 5 = 2 |
We will develop a little program to demonstrate a
couple of these operators. A Program for Expressions. The following Java applet just requests that the user input two different numbers. It then parses them as floats and casts them to integers. Then it computes their quotient and modulus and displays the result. You need to call the file ExpressionsOperators.java. |
/* applet to investigate the division and modulus operators for integers and floats */ import javax.swing.JOptionPane; import javax.swing.JTextArea; import javax.swing.JApplet; public class ExpressionsOperators extends JApplet {
} // end class ExpressionsOperator |
The following section will describe the code.
You will also need to create the following ExpressionsOperators.html. |
<APPLET CODE="ExpressionsOperators.class" WIDTH=200 HEIGHT=100> </APPLET> |
After compiling and executing you should try the
following inputs.
The output for the first case is displayed below. Notice how the integers are cast and the integer division gives a rather surprising result. Computers are programmed to deal with integers in this way. |
Obviously, we can develop far more complicated mathematical expressions using parentheses. One that is probably more beastly than you have ever seen is contained in the following image. |
That complicated capital sigma is actually a summation.
But, even mathematical expressions of this degree of complexity can be
computed if the coder is adequately systematic. Now we will describe some other operators and operations that can be used to develop mathematical expressions. We will make use of these in future web pages. Assignments and Increments/Decrements. You may have already encountered how an equal sign = is really an assignment operator in computing rather than an equal sign. Thus statements like x = 23.4 * ( x + 11.2 ) actually make sense. Whatever, x currently is you should add 11.2 to it and then multiply this sum by 23.4 to get the "new value" of x. In Java, there are several other ways that certain fairly standard assignment operations can be performed. The following table contains four of them. |
Expression | Meaning |
x += a | x = x + a |
x -= a | x = x - a |
x *= a | x = x * a |
x /= a | x = x / a |
I have a strong tendency to not use these shorthand
references because they can make code just that little bit more obscure,
but you are free to use them. It is also possible to increment and decrement a variable by one in a shorthand notation as shown in the following table. |
Expression | Meaning |
x ++ | x = x + 1 |
x -- | x = x - 1 |
This notation is much more common and I will use it. Comparisons. Everyone that has reached college has seen things like > and < in order to make comparisons. It is not very much different in Java. The following table contains the basic comparison operators for Java. |
Comparison Operators | ||
Operator |
Meaning |
Example |
< | less than | 4 < 7.6 |
<= | less than or equal | 3 <= 11 |
> | greater than | 12.34 > 12.33 |
>= | greater than or equal | 3.14159 >= 3.14159 |
== | equal | "fox" == "fox" |
!= | not equal | "fox" != "Fox" |
Logical Operators.
While there are more logical operators than we will discuss, particularly
because we aren't going to get into bit level operations, we will present
two that you will need throughout the rest of this semester and beyond. The operators are in the following table. We will give a few examples after the table. |
Operator | Meaning |
&& | logical AND |
| | | logical OR |
Let's say you want something to be able to satisfy more
than one comparison condition. Maybe you want your significant other
to be more than 60" tall and less than 70" tall. The condition you
might use to search through a database would look something like the
following. ( height > 60 ) && ( height < 70 ) If you don't care if they are exactly at the limits then the relevant expression would be ( height >= 60 ) && ( height <= 70 ) Or maybe you don't care what color their hair is. This might be developed as the following. ( hairColor == "Blonde") | | ( hairColor == "Brown") | | ( hairColor == "Black") | | ( hairColor == "Red") Well, we could go on and on with this. But the next few webpages will focus on code that makes use of mathematical expressions and comparisons in order to make decisions. |