The Simple Assignment Operator
One of the most common operators that you'll encounter is the simple assignment operator "=". It assigns the value on its right to the operand on its left:
int cadence = 0; int speed = 0; int gear = 1;
IMPORTANT NOTICE: The equal sign in this case is not the equal sign you are used to using in a math class. It should be read as "becomes" and not "equals". For example, you can write the code score=score+10; in Java and it is perfectly acceptable. In math there is no number that equals itself plus 10 more. In Java the code would be read as "The variable named score becomes what it is currently plus 10 more." The Arithmetic Operators
The Java programming language provides operators that perform addition, subtraction, multiplication, and division. There's a good chance you'll recognize them by their counterparts in basic mathematics. The only symbol that might look new to you is "%", which divides one operand by another and returns the remainder as its result. Operator | Function | + | additive operator (also used for String concatenation) | - | subtraction operator | * | multiplication operator | / | division operator | % | remainder operator |
The following program, ArithmeticDemo, tests the arithmetic operators.
class ArithmeticDemo { public void doSomeMath() { int result = 1 + 2; // result is now 3 System.out.println(result);
result = result - 1; // result is now 2 System.out.println(result);
result = result * 2; // result is now 4 System.out.println(result);
result = result / 2; // result is now 2 System.out.println(result);
result = result + 8; // result is now 10 result = result % 7; // result is now 3 System.out.println(result); } }
You can also combine the arithmetic operators with the simple assignment operator to create compound assignments. For example, x+=1; and x=x+1; both increment the value of x by 1.
The + operator can also be used for concatenating (joining) two strings together, as shown in the following ConcatDemo program:
class ConcatDemo { public void letsConcat() { String firstString = "This is"; String secondString = " a concatenated string."; String thirdString = firstString+secondString; System.out.println(thirdString); } }
By the end of this program, the variable thirdString contains "This is a concatenated string.", which gets printed to standard output.
The Unary Operators
The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.
Operator | Name; Function | + | Unary plus operator; indicates positive value (numbers are positive without this, however) | - | Unary minus operator; negates an expression | ++ | Increment operator; increments a value by 1 | -- | Decrement operator; decrements a value by 1 | ! | Logical complement operator; inverts the value of a boolean |
The following program, UnaryDemo, tests the unary operators:
class UnaryDemo { public void unaryFun() { int result = +1; // result is now 1 System.out.println(result); result--; // result is now 0 System.out.println(result); result++; // result is now 1 System.out.println(result); result = -result; // result is now -1 System.out.println(result); boolean success = false; System.out.println(success); // false System.out.println(!success); // true } }
The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code result++; and ++result; will both end in result being incremented by one. The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value. If you are just performing a simple increment/decrement, it doesn't really matter which version you choose. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.
The following program, PrePostDemo, illustrates the prefix/postfix unary increment operator:
class PrePostDemo { public void prepostFun() { int i = 3; i++; System.out.println(i); // "4" ++i; System.out.println(i); // "5" System.out.println(++i); // "6" System.out.println(i++); // "6" System.out.println(i); // "7" } }
For each of the Programming 1 lessons you are strongly encouraged to view and play around with the sample code provided in the BlueJ Java Student Samples folder. Open BlueJ and choose Open Project from the Project menu. Navigate to your Programming 1 folder on the I: drive and open the BlueJ Java Student Samples folder. Open the project named Arithmetic. Lesson Example: Project Arithmetic, Class ArithmeticDemo, methods simpleMath(), whole_division() and double_division() - Right-click on the Class named ArithmeticDemo and select new ArithmeticDemo()
- Right-click on the red rectangle and choose the method named simpleMath()
- Right-click on the red rectangle and choose the method named whole_division()
- Right-click on the red rectangle and choose the method named double_division()
Code for method simpleMath() void simpleMath() { int result = 1; result = 1 + 2; // result is now 3 System.out.println(result);
result = result - 1; // result is now 2 System.out.println(result);
result = result * 2; // result is now 4 System.out.println(result);
result = result / 2; // result is now 2 System.out.println(result);
result = result + 8; // result is now 10 result = result % 7; // result is now 3 System.out.println(result); }
Code for method whole_division() void whole_division() { int num1=10,num2=3,whole,remain; whole=num1/num2; remain=num1%num2; System.out.println(num1 + " divided by " + num2 + " = " + whole); System.out.println("The remainder of " + num1 + " divided by " + num2 + " = " + remain); }
Code for method double_division() void double_division() { double num1=10,num2=3,answer; answer=num1/num2; System.out.println(num1 + " divided by " + num2 + " = " + answer); answer=Math.round(answer*100.0)/100.0; System.out.println("Formatted to 2 decimal places: " + num1 + " divided by " + num2 + " = " + answer); }
|