answer1is 6 because 2 + 4 = 6answer2is 2 because 6 / 3 = 2answer3is 7 because 10 - 3 = 7
Operators
In Java, we use operators to change or compare the values of variables. There are four different types of operators, which are:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
Operators are commonly used to change the value of a variable in a loop or conditional. These topics will be covered in the next sections!
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 |
Operators in Java follow the order of operations (known as PEMDAS).
Operators can be used when creating variables or when changing a variable’s value after it was initialized. In the example below, we have three variables whose values are set using different operators. Without running the code, think about what value would each variable hold.
int answer1 = 2 + 4;int answer2 = 6 / 3;int answer3 = 10 - 3;Answer
In the example below, we have an integer variable named magicNumber 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 magicNumber?
int magicNumber = 6;System.out.print(magicNumber * 2);Answer
magicNumber’s new value is 12. This is because magicNumber holds
the value 6. When multiplied by 2, 6 becomes 12, which is the new
value of magicNumber.
When dividing two integers, the result will be an integer instead of a
double. Example: System.out.print(7 / 2); will print out 3. If you want
the result to return a double, like 3.5, the data type should be a
double instead of an int.
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 | 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 | 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 | 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 | a /= 6 |
Assignment operators are similar to arithmetic operators. The main difference is that assignment operators are the shorthand version. Using assignment operators can help make code easier to read. It prevents having to write the variable name twice which can also be helpful in preventing code errors.
In the example below, we have variables a which is set to 10, and variable b which is set to 5. Since += adds 1 to a, 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 4Sometimes placed in the category of “unary operators”, the ++ and -- operators are also used to change the value of the a variable.
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.
| Operator | Description | Example | |
|---|---|---|---|
++ | Increment: add 1 to a variable | int, double | a++ |
-- | Decrement: subtracts 1 to a variable | int, double | a-- |
In the example below, we have two variables: x which is set to 6 and y which is set to 7.
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.
Comparison 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 and are a main part of conditionals, which are discussed later in this stage.
| Operator | Description | Example |
|---|---|---|
== | Equal 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 to: Checks if one value is greater than or equal to the other | a >= b |
<= | Less than or equal to: Checks if one value is less than or equal to the other | a <= b |
Comparison operators help 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: c and d.
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(c > d);Logical Operators
Section titled “Logical Operators”Logical operators help a program make decisions by using true or false statements. They are also used in conditionals.
Logical operators also follow the order of operations (known as PEMDAS) and parentheses can be used to change this order.
| 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: fiveIsGreaterThanThree and nineIsLessThanTwo.
We know fiveIsGreaterThanThree is true since 5 is greater than 3. We also know that nineIsLessThanTwo is false since 9 is not less than 2.
Using that information and without running the code, what should the print statements print out?
boolean fiveIsGreaterThanThree = 5 > 3; // Trueboolean nineIsLessThanTwo = 9 < 2; // False
System.out.println(fiveIsGreaterThanThree && nineIsLessThanTwo);System.out.println(fiveIsGreaterThanThree || nineIsLessThanTwo);System.out.println(!fiveIsGreaterThanThree);Answer
false. && returns true if both statements are true
nineIsLessThanTwois false, therefore the operator will return false.true. || returns true if one of the statements are true
fiveIsGreaterThanThreeis true, therefore the the operator will return true.false. ! reverses the outcome
fiveIsGreaterThanThreeis true, adding ! will reverse it and return false.