1. Overview

Operators are a fundamental building block of any programming language. We use operators to perform operations on values and variables.

Java provides many groups of operators. They are categorized by their functionalities.

In this tutorial, we’ll walk through all Java operators to understand their functionalities and how to use them.

2. Arithmetic Operators

We use arithmetic operators to perform simple mathematical operations. We should note that arithmetic operators only work with primitive number types and their boxed types, such as int and Integer.

Next, let’s see what operators we have in the arithmetic operator group.

2.1. The Addition Operator

The addition operator (+) allows us to add two values or concatenate two strings:

int ten = 5 + 5;
String youAndMe = "You " + "and" + " Me";

2.2. The Subtraction Operator

Usually, we use the subtraction operator (-) to subtract one value from another:

int five = 10 - 5;
int negative80 = 20 - 100;

2.3. The Multiplication Operator

The multiplication operator (*) is used to multiply two values or variables:

int hundred = 20 * 5;
int fifteen = -5 * -3;

2.4. The Division Operator

The division operator (/) allows us to divide the left-hand value by the right-hand one:

int four = 20 / 5;
int seven = 15 / 2;

When we use the division operator on two integer values (byte, short, int, and long), we should note that the result is the quotient value. The remainder is not included.

As the example above shows, if we calculate 15 / 2, the quotient is 7, and the remainder is 1. Therefore, we have 15 / 2 = 7.

2.5. The Modulo Operator

We can get the quotient using the division operator. However, if we just want to get the remainder of a division calculation, we can use the modulo operator (%):

int one = 15 % 2;
int zero = 10 % 5;

3. Unary Operators

As the name implies, unary operators only require one single operand. For example, we usually use unary operators to increment, decrement, or negate a variable or value.

Now, let’s see the details of unary operators in Java.

3.1. The Unary Plus Operator

The unary plus operator (+) indicates a positive value. If the number is positive, we can omit the ‘+’ operator:

int five = +5; // same as: int five = 5

3.2. The Unary Minus Operator

Opposite to the unary plus operator, the unary minus operator (-) negates a value or an expression:

int negativeFive = -5;
int eighty = -(20 - 100);

3.3. The Logical Complement Operator

The logical complement operator (!) is also known as the “NOT” operator. We can use it to invert the value of a boolean variable or value:

boolean aTrue = true;
boolean bFalse = !aTrue;

3.4. The Increment Operator

The increment operator (++) allows us to increase the value of a variable by 1:

int number = 5;
number++; // number = 6

3.5. The Decrement Opeartor

The decrement operator (–) does the opposite of the increment operator. It decreases the value of a variable by 1:

int number = 5;
number--; // number = 4

We should keep in mind that the increment and decrement operators can only be used on a variable. For example, “*int a = 5; a++;” is fine. However, the expression “5++*” won’t be compiled.

4. Relational Operators

Relational operators can be called “comparison operators” as well. Basically, we use these operators to compare two values or variables.

4.1. The “Equal To” Operator

We use the “equal to” operator (==) to compare the values on both sides. If they’re equal, the operation returns true:

int number1 = 5;
int number2 = 5;
boolean theyAreEqual = number1 == number2;

The “equal to” operator is pretty straightforward. On the other hand, the Object class has provided the equals() method. As the Object class is the superclass of all Java classes, all Java objects can use the equals() method to compare each other.

When we want to compare two objects – for instance, when we compare Long objects or compare Strings – we should choose between the comparison method from the equals() method and that of the “equal to” operator wisely.

4.2. The “Not Equal To” Operator

The “not equal to” operator (!=) does the opposite of the ‘==’ operator. If the values on both sides are not equal, the operation returns true:

int number1 = 5;
int number2 = 7;
boolean notEqual = number1 != number2;

4.3. The “Greater Than” Operator

When we compare two values with the “greater than” operator (>), it returns true if the value on the left-hand side is greater than the value on the right-hand side:

int number1 = 7;
int number2 = 5;
boolean greater = number1 > number2;

4.4. The “Greater Than or Equal To” Operator

The “greater than or equal to” operator (>=) compares the values on both sides and returns true if the left-hand side operand is greater than or equal to the right-hand side operand:

int number1 = 7;
int number2 = 5;
boolean greaterThanOrEqualTo = number1 >= number2;
number1 = 5;
greaterThanOrEqualTo = number1 >= number2;

4.5. The “Less Than” Operator

The “less than” operator (<) compares two values on both sides and returns true if the value on the left-hand side is less than the value on the right-hand side:

int number1 = 4;
int number2 = 5;
boolean lessThan = number1 < number2;

4.6. The “Less Than or Equal To” Operator

Similarly, the “less than or equal to” operator (<=) compares the values on both sides and returns true if the left-hand side operand is less than or equal to the right-hand side:

int number1 = 4;
int number2 = 5;
boolean lessThanOrEqualTo = number1 <= number2;
number1 = 5;
lessThanOrEqualTo = number1 <= number2;

5. Logical Operators

We have two logical operators in Java: the logical AND and OR operators. Basically, their function is pretty similar to the AND gate and the OR gate in digital electronics.

Usually, we use a logical operator with two operands, which are variables or expressions that can be evaluated as boolean.

Next, let’s take a closer look at them.

5.1. The Logical AND Operator

The logical AND operator (&&) returns true only if both operands are true:

int number1 = 7;
int number2 = 5;
boolean resultTrue = (number1 > 0) && (number1 > number2);

5.2. The Logical OR Operator

Unlike the ‘*&&‘ operator, the logical OR operator (||*) returns true if at least one operand is true:

int number1 = 7;
int number2 = 5;
boolean resultTrue = (number1 > 100) || (number1 > number2);

We should note that the logical OR operator has the short-circuiting effect: It returns true as soon as one of the operands is evaluated as true, without evaluating the remaining operands.

6. Ternary Operator

A ternary operator is a short form of the if-then-else statement. It has the name ternary as it has three operands. First, let’s have a look at the standard if-then-else statement syntax:

if ( condition ) {
    expression1
} else {
    expression2
}

We can convert the above if-then-else statement into a compact version using the ternary operator:

Let’s look at its syntax:

condition ? expression1 : expression2

Next, let’s understand how the ternary operator works through a simple example:

int number = 100;
String greaterThan50 = number > 50 ? "The number is greater than 50" : "The number is NOT greater than 50";

7. Bitwise and Bit Shift Operators

As the article “Java bitwise operators” covers the details of bitwise and bit shift operators, we’ll briefly summarize these operators in this tutorial.

7.1. The Bitwise AND Operator

The bitwise AND operator (&) returns the bit-by-bit AND of input values:

int number1 = 12;
int number2 = 14;
int twelve = number1 & number2; // 00001100 & 00001110 = 00001100 = 12

7.2. The Bitwise OR Operator

The bitwise OR operator (|) returns the bit-by-bit OR of input values:

int number1 = 12;
int number2 = 14;
int fourteen = number1 | number2; // 00001100 | 00001110 = 00001110 = 14

7.3. The Bitwise XOR Operator

The bitwise XOR (exclusive OR) operator (^) returns the bit-by-bit XOR of input values:

int number1 = 12;
int number2 = 14;
int two = number1 ^ number2; // 00001100 ^ 00001110 = 00000010 = 2

7.4. The Bitwise Complement Operator

The bitwise complement operator (~) is a unary operator. It returns the value’s complement representation, which inverts all bits from the input value:

int number = 12;
int negative13 = ~number; // ~00001100 = 11110011 = -13

7.5. The Left Shift Operator

Shift operators shift the bits to the left or right by the given number of times.

The left shift operator (<<) shifts the bits to the left by the number of times defined by the right-hand side operand. After the left shift, the empty space in the right is filled with 0.

Next, let’s left shift the number 12 twice:

int fourtyeight = 12 << 2; // 00001100 << 2 = 00110000 = 48

n << x has the same effect of multiplying the number n with x power of two.

7.6. The Signed Right Shift Operator

The signed right shift operator (>>) shifts the bits to the right by the number of times defined by the right-hand side operand and fills 0 on voids left as a result.

We should note that the leftmost position after the shifting depends on the sign extension.

Next, let’s do “signed right shift” twice on the numbers 12 and -12 to see the difference:

int three = 12 >> 2; // 00001100 >> 2 = 00000011 = 3
int negativeThree = -12 >> 2; // 11110100 >> 2 = 11111101 = -3

As the second example above shows, if the number is negative, the leftmost position after each shift will be set by the sign extension.

n >> x has the same effect of dividing the number n by x power of two.

7.7. The Unsigned Right Shift Operator

The unsigned right shift operator (>>>) works in a similar way as the ‘>>’ operator. The only difference is that after a shift, the leftmost bit is set to 0.

Next, let’s unsigned right shift twice on the numbers 12 and -12 to see the difference:

int three = 12 >>> 2; // 00001100 >> 2 = 00000011 = 3
int result = -12 >>> 2; // result = 1073741821 (11111111111111111111111111110100 >>> 2 = 00111111111111111111111111111101)

As we can see in the second example above, the >>> operator fills voids on the left with 0 irrespective of whether the number is positive or negative.

8. The “instanceof” Operator

Sometimes, when we have an object, we would like to test if it’s an instance of a given type. The “instanceof” operator can help us to do it:

boolean resultTrue = Long.valueOf(20) instanceof Number;

9. Assignment Operators

We use assignment operators to assign values to variables. Next, let’s see which assignment operators we can use in Java.

9.1. The Simple Assignment Operator

The simple assignment operator (=) is a straightforward but important operator in Java. Actually, we’ve used it many times in previous examples. It assigns the value on its right to the operand on its left:

int seven = 7;

9.2. Compound Assignments

We’ve learned arithmetic operators. We can combine the arithmetic operators with the simple assignment operator to create compound assignments.

For example, we can write “a = a + 5” in a compound way: “a += 5“.

Finally, let’s walk through all supported compound assignments in Java through examples:

// Assuming all variables (a,b,c,d,e) have the initial value 10
a += 4; // a = 14, same as a = a + 4
b -= 4; // b = 6, same as b = b - 4
c *= 4; // c = 40, same as c = c * 4
d /= 4; // d = 2, same as d = d / 4
e %= 4; // e = 2, same as e = e % 4

10. Conclusion

Java provides many groups of operators for different functionalities. In this article, we’ve passed through the operators in Java.