- answer1 is 6 because 2 + 4 = 6 * answer2 is 2 because 6 / 3 = 2 * answer3 is 7 because 10 - 3 = 7
Operators
In Java, we use operators to perform to change or compare the values of variables. There are different types of operators, which are:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
These types of operators are often used in programming. They can be used to update a robot arm’s setpoint, where we want the mechanism to be, or to change the value of a variable in a loop. Operators are important to know when programming your robot
Arithmetic Operators
Section titled “Arithmetic Operators”Arithmetic operators are used to perform basic math on variables. These include:
| Operator | Description | Compatible Data Types | Example |
|---|---|---|---|
| + | Addition: Adds two values together | int, double | a + b |
| - | Subtraction: Subtracts one value from the other | int, double | a - b |
| * | Multiplication: Multiplies two values together | int, double | a * b |
| / | Division: Divides one value from the other | int, double | a / b |
| % | Remainder: Returns the remainder of two numbers after division | int, double | a / b |
| ++ | Increment: add 1 to a variable | int, double | a++ |
| — | Decrement: subtracts 1 to a variable | int, double | a— |
Similar to with arithmetic, operators in Java follow the order of operations (known as PEMDAS) and parentheses can be used to change this order.
Operators can be used when creating variables or when changing a variable later in code. In the example below, we have three variables whose values are set using different operators. Without running the code, what value would each variable hold?
int answer1 = 2 + 4;int answer2 = 6 / 3;int answer3 = 10 - 3;Answer
Operators can also be used to change a variable later in code. In the example below, we have an integer variable named Number that is set to 6. Using the multiplication operator, the value is adjusted inside the print statement. Without running the code, what is the value of Number?
int Number = 6;System.out.print(Number * 2);Answer
Number’s new value is 12. This is because Number holds the value 6. When multiplied by 2, 6 becomes 12, which is the new value of Number
When dividing two integers, the result will be an integer instead of a
double. Example: java System.out.print(7 / 2); The example above will
print out 3. If you want the result to return a double, the data type should
be a double instead of an int.
The ++ and — operators are often used to change the value of the code as well. They help with incrementing and decrementing values and are used in conditionals (which are covered in a later stage). For now, we can use the operators to change variables without using conditionals. Like mentioned before, ++ increments the value of a variable by adding 1 and — decrements the value of a variable by subtracting 1.
In the example below, we have two variables.
int x = 6;int y = 7;
System.out.println(++x);System.out.println(--y);In the first print statement, we have ++x. This means it takes the value of x and increments it by 1 which gives us 7 because 6 + 1 is 7. In the second print statement, we have —y. This means that it takes the value of y, decrements it by 1 which gives us 6 since 7 - 1 is 6.
Assignment Operators
Section titled “Assignment Operators”Assignment operators are used when assigning or updating the values in a variable. These include:
| Operator | Description | Example |
|---|---|---|
| = | Assigns a value to a variable | a = 2 |
| += | Addition: Takes the current value of the variable, adds the stated amount then assigns the result to the variable. It’s the same as x = x + y | a += 3 |
| -= | Subtraction: Takes the current value of the variable, subtracts the stated amount then assigns the result to the variable. It’s the same as x = x - y | a -= 4 |
| *= | Multiplication: Takes the current value of the variable, multiplies the stated amount then assigns the result to the variable. It’s the same as x = x * y | a *= 5 |
| /= | Division: Takes the current value of the variable, divides the stated amount then assigns the result to the variable. It’s the same as x = x / y | a /= 6 |
Assignment Operators are similar to Arithmetic Operators, they are shorthand. Using the shorthand version can help make code easier to read. It prevents having to write the variable name twice which can also be helpful in preventing code issues.
In the example below, we have variables A which is set to 10, and variable B which is set to 5. Since += adds 1, A will hold the value of 11. Similarly, B will now hold the value of 4
int A = 10;int B = 5;A += 1;B -= 1;
System.out.println(A); // prints 11System.out.println(B); // prints 4Comparison Operators
Section titled “Comparison Operators”Comparison operators are symbols that tell the program how to compare values. They can be used to help make decisions, which is an important part of programming. Comparison operators are a main part of conditionals, which are discussed later in this stage.
| Operator | Description | Example |
|---|---|---|
| == | Equals to: Checks if one value is the same values as the other | a == b |
| != | Not Equal: Checks if one value is NOT equal to the other | a != b |
| > | Greater than: Checks if one value is greater than the other | a > b |
| < | Less than: Checks if one value is less than the other | a < b |
| >= | Greater than or equal too: Checks if one value is greater than or equal to the other | a >= b |
| <= | Less than or equal too: Checks if one value is less than or equal to the other | a <= b |
Comparison Operators help programs make decisions because they can return if the value of a comparison is true or false. If you remember from the previous section, these are booleans!
Comparison operators are very similar to what you see in math problems. In this example, we have two variables: A and B. C is set to 2, and D is set to 4. The print statement compares the two variables using the greater than sign. In the example below, it returns false because 2 is not greater than 4.
int C = 2;int D = 4;System.out.print(A > B);Logical Operators
Section titled “Logical Operators”Logical operators are symbols that let a program make decisions by combining true or false statements. They are also used when making decisions in a program and are also used in conditionals.
| Operator | Description | Example |
|---|---|---|
| && | and: true if both statements are true | true && true // this is true |
| || | Or: true if at least one statement is true | true || false // this is true |
| ! | Not: reverses. If true, then false. If false then true | !true // this changes to false |
In the example below we have 2 variables: AnswerOne and AnswerTwo. We know AnswerOne is true and AnswerTwo is false. Using that information and without running the code, what should the print statements print out?
boolean AnswerOne = 5 > 3; // Trueboolean AnswerTwo = 9 < 2; // False
System.out.println(AnswerOne && AnswerTwo);System.out.println(AnswerOne || AnswerTwo);System.out.println(!AnswerOne);Answer
- false. && returns true if both statements are true. AnswerTwo is false, therefore the operator will return false * true. || returns true if one of the statements are true. AnswerOne is true, therefore the the operator will return true * false. ! reverses the outcome. AnswerOne is true, adding ! will reverse it and return false