Skip to content

Operators

In Java, operators are used to change or compare the values of variables. There four different types of operators are:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators

Arithmetic operators are used to perform basic math on variables. These include:

OperatorDescriptionExample
+Addition: Adds two values togethera + b
-Subtraction: Subtracts one value from the othera - b
*Multiplication: Multiplies two values togethera * b
/Division: Divides one value from the othera / b
%

Remainder: Returns the remainder of two numbers after division

a % b

Operators can be used anywhere you would use a number, such as when assigning variables. 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

  • answer1 is 6 because 2 + 4 = 6
  • answer2 is 2 because 6 / 3 = 2
  • answer3 is 7 because 10 - 3 = 7

Note

When dividing two integers, the result will be a rounded down 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.

Due to this, it’s better to use doubles when writing code because it’s more precise.

Assignment operators are used when assigning or updating the values in a variable. These include:

OperatorDescriptionExample
=Assigns a value to a variablea = 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. 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 2 to a, a will hold the value of 12. Similarly, b will now hold the value of 4

int a = 10;
int b = 5;
a += 2;
b -= 1;
System.out.println(a); // prints 12
System.out.println(b); // prints 4

Sometimes 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.

OperatorDescriptionExample
++Increment: adds 1 to a variablea++
--Decrement: subtracts 1 from a variablea--

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;
x++; // x is now 7!
y--; // y is now 6!
System.out.println(x); // prints 7
System.out.println(y); // prints 6

Comparison operators compare values. They can be used to help make decisions and are a main part of conditionals, which are discussed later in this stage.

OperatorDescriptionExample
==

Equal to: Checks if two values are the same

a == b
!=Not Equal: Checks if two values are not the samea != b
>Greater than: Checks if one value is greater than the othera > b
<Less than: Checks if one value is less than the othera < 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.println(c > d); // prints false
Note

== and != can not be used to compare Strings. To compare Strings, .equals() is used. Though it’s not used, more information can be found here.

Similarly, >, <, >=, and <= also can not be used for Strings. To compare if String, .compareTo() is used. More information can be found here.

When using comparison operators, make sure to use a double equal sign == instead of a single equal =. Unlike in math, a single equal is assignment.

In the example below, the print statement would not run. You would get an error message about how the left hand side of an assignment must be a variable.

System.out.println(6 = 8);

Adding the double equal sign, fixes the error because the compiler will read this as comparing the value on the left to the value on the right. This will print out false because 6 is not equal to 8.

System.out.println(6 == 8);

Logical operators help a program make decisions by using true or false statements. They are also used in conditionals.

Note

Logical operators also follow the order of operations (known as PEMDAS) and parentheses can be used to change this order.

OperatorDescriptionExample
&&And: true if both statements are truetrue && true // this is true
||Or: true if at least one statement is truetrue || 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; // true
boolean 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 nineIsLessThanTwo is false, therefore the operator will return false.

  • true. || returns true if one of the statements are true fiveIsGreaterThanThree is true, therefore the the operator will return true.

  • false. ! reverses the outcome fiveIsGreaterThanThree is true, adding ! will reverse it and return false.

Operators can be used to assign new values to existing variables. Arithmetic, and Assignment operators can help perform math and change the values.

Operators can also change the value of variables using the = sign. In the example, e’s value changes to 12 in the print statement. e’s value is being set to whatever is on the right side of the =. In this case, it’s 12 because f is equal to 2, so 2 + 10 is 12.

int e = 0;
int f = 2;
System.out.println(e = f + 10);

In the example, we have an integer variable named magicNumber that is set to 6. Using the multiplication operator, the value is adjusted inside the print statement.

int magicNumber = 6;
System.out.println(magicNumber * 2);

Unlike the previous example, magicNumber’s value remains 6. The value calculated inside println only affects what gets printed, since it’s never assigned back to magicNumber using an equal sign. This can be confirmed by printing the value of magicNumber again.

Exercise
WIP