Operators are an essential part of programming in Java and any other language. They are special characters that carry out particular actions on one or more values. These actions may include manipulating data and controlling the flow of a program. Operators allow developers to manipulate and modify data as needed.

In Java, there are many different types of operators, including arithmetic, assignment, relational, logical, and bitwise operators, to name just a few.

In this article, we will take a closer look at the various operators in Java and how they are used to perform various operations on data. We will explore their syntax, precedence, and associativity, and we will provide examples of how to use them in different types of Java programs. By the end of this article, you will have a solid understanding of the different operators in Java and how to use them effectively in your own code.

## Arithmetic Operators in Java

Arithmetic operators in Java are used to perform basic arithmetic operations, such as addition, subtraction, multiplication, and division.

Here is a table of arithmetic operators in Java, along with their name, description, and example:

Operator | Name | Description | Example |
---|---|---|---|

+ | Addition | The addition operator adds two values together and returns the result. | int x = 5 + 3; |

– | Subtraction | The subtraction operator subtracts one value from another and returns the result. | int x = 5 – 3; |

* | Multiplication | The multiplication operator multiplies two values together and returns the result. | int x = 5 * 3; |

/ | Division | The division operator divides one value by another and returns the result. | int x = 5 / 3; |

% | Modulus | The modulus operator returns the remainder of a division operation. It can be used to determine if a number is odd or even, for example. | int x = 5 % 3; |

++ | Increment | The purpose of the increment operator is to add 1 to the value of the operand. It can be used in two ways: either before the operand (prefix) or after the operand (postfix). | int x = 5; x++; |

— | Decrement | The decrement operator decreases the value of the operand by 1. It can be placed before (prefix) or after (postfix) the operand. | int x = 5; x–; |

It is crucial to be aware that the sequence in which operations are performed, also referred to as operator precedence, determines which operations are carried out first in an expression.

For example, multiplication and division are performed before addition and subtraction, unless parentheses are used to specify a different order.

### Examples

Here are some examples of how arithmetic operators work in Java:

// Addition int x = 5 + 3; // x will be 8 // Subtraction int x = 5 - 3; // x will be 2 // Multiplication int x = 5 * 3; // x will be 15 // Division int x = 5 / 3; // x will be 1 // Modulus (remainder) int x = 5 % 3; // x will be 2

In Java, the increment operator (++) and decrement operator (–) are unary operators that add or subtract 1 from the value of a variable, respectively. These operators can be placed either before or after the variable being modified.

Here are some examples of how the increment and decrement operators work in Java:

// Increment operator int x = 5; x++; // x is now 6 ++x; // x is now 7 // Decrement operator int y = 10; y--; // y is now 9 --y; // y is now 8

In prefix form, the operation is carried out before the operand is used in the expression, with the operator being placed before it. In contrast, in postfix form, the operator is placed after the operand and the operation is performed after the value is utilized in the expression.

Here is an example that demonstrates the difference between prefix and postfix form:

int x = 5; int y = ++x; // x is 6, y is 6 int z = x++; // x is 7, z is 6

In this example, the value of x is incremented to 6 before it is assigned to y, so y is also 6. The value of x is then incremented to 7, but it is used in the expression before the operation is performed, so z is assigned the original value of x, which was 6.

**The increment and decrement operators have a higher precedence than most other operators**, so expressions involving them may not need to be parenthesized. However, it is a good idea to use parentheses to make the code more readable and to avoid confusion.

## Assignment Operators in Java

Assignment operators are used to assign a value to a variable or to modify the value of a variable. The basic assignment operator is the equal sign (=), which assigns the value on the right to the variable on the left. For example:

int x; x = 5; // x is now 5

Assignment operators allow you to perform an operation and assign the result to a variable in a single step. For example, the += operator can be used to add a value to a variable and assign the result back to that same variable in one go. This is demonstrated in the example provided.

Using assignment operators in conjunction with other operators can save time and make your code more efficient.

Here is a table of assignment operators in Java, along with their name, description, and example:

Operator | Name | Description | Example |
---|---|---|---|

= | Assignment | The assignment operator assigns a value to a variable. It is used to give a value to a variable for the first time or to change its value. | int x = 5; |

+= | Addition assignment | The addition assignment operator adds a value to a variable and assigns the result to the variable. It is a shorthand form of the expression “x = x + y”. | int x = 5;
x += 3; |

-= | Subtraction assignment | The subtraction assignment operator subtracts a value from a variable and assigns the result to the variable. It is a shorthand form of the expression “x = x – y”. | int x = 5;
x -= 3; |

*= | Multiplication assignment | The multiplication assignment operator multiplies a value by a variable and assigns the result to the variable. It is a shorthand form of the expression “x = x * y”. | int x = 5;
x *= 3; |

/= | Division assignment | The division assignment operator divides a variable by a value and assigns the result to the variable. It is a shorthand form of the expression “x = x / y”. | int x = 5;
x /= 3; |

%= | Modulus assignment | The modulus assignment operator calculates the remainder of a division operation and assigns the result to the variable. It is a shorthand form of the expression “x = x % y”. | int x = 5;
x %= 3; |

&= | Bitwise AND assignment | The bitwise AND assignment operator performs a bitwise AND operation on two values and assigns the result to the variable. It is a shorthand form of the expression “x = x & y”. | int x = 5;
x &= 3; |

^= | Bitwise XOR assignment | The bitwise XOR assignment operator performs a bitwise XOR operation on two values and assigns the result to the variable. It is a shorthand form of the expression “x = x ^ y”. | int x = 5;
x ^= 3; |

= | Bitwise OR assignment | The bitwise OR assignment operator performs a bitwise OR operation on two values and assigns the result to the variable. It is a shorthand form of the expression “x = x | int x = 5;
x |= 3 |

<<= | Left shift assignment | The left shift assignment operator shifts the bits of a value to the left by a specified number of places and assigns the result to the variable. It is a shorthand form of the expression “x = x << y”. | int x = 5;
x <<= 2; |

>>= | Right shift assignment | The right shift assignment operator shifts the bits of a value to the right by a specified number of places and assigns the result to the variable. It is a shorthand form of the expression “x = x >> y”. | int x = 5;
x >>= 2; |

>>>= | Unsigned right shift assignment | The unsigned right shift assignment operator shifts the bits of a value to the right by a specified number of places and assigns the result to the variable. It is a shorthand form of the expression “x = x >>> y”. This operator is similar to the right shift operator, but it always fills the empty bits with 0s, regardless of the sign of the value. | int x = 5;
x >>>= 2; |

**The assignment operator has a lower precedence than most other operators,** so expressions that use multiple assignment operators will be evaluated from right to left. For example, in the expression “x = y = z = 0”, the value 0 will be assigned to z first, then y, and finally x.

### Examples

Here are some examples of how to use assignment operators in Java:

// Basic assignment int x; x = 5; // x is now 5 // Add and assign int x = 5; x += 3; // x is now 8 // Subtract and assign int x = 5; x -= 3; // x is now 2 // Multiply and assign int x = 5; x *= 3; // x is now 15 // Divide and assign int x = 5; x /= 3; // x is now 1 // Modulus and assign int x = 5; x %= 3; // x is now 2

## Logical Operators in Java

In Java, logical operators are used to manipulate boolean values through logical operations. There are three logical operators in Java: AND (&&), OR (||), and NOT (!). They are often used in conditional statements, such as **if** and **while** loops, to control the flow of a program based on certain conditions.

Operator | Name | Description | Example |
---|---|---|---|

&& | Logical AND | The logical AND operator returns true if both operands are true, and false otherwise. | boolean x = true && true; |

|| | Logical OR | The OR operator will evaluate to true if at least one of the operands is true, and false if both operands are false. | boolean x = true || false; |

! | Logical NOT | The logical NOT operator negates the value of a boolean expression. If the expression is true, it returns false, and if it is false, it returns true. | boolean x = !true; |

It’s important to note that the **logical AND operator (&&) and the logical OR operator (||) have a lower precedence than the logical NOT operator (!),** so expressions involving these operators may need to be parenthesized to ensure that they are evaluated correctly.

Here is a table showing how the logical operators work in Java:

A | B | A && B | A || B | !A |
---|---|---|---|---|

false | false | false | false | true |

true | false | false | true | false |

false | true | false | true | true |

true | true | true | true | false |

### Examples

Look at some examples of how the logical operators work in Java:

// AND boolean result = true && true; // result will be true result = true && false; // result will be false result = false && true; // result will be false result = false && false; // result will be false // OR result = true || true; // result will be true result = true || false; // result will be true result = false || true; // result will be true result = false || false; // result will be false // NOT result = !true; // result will be false result = !false; // result will be true

The AND operator (&&) returns true if both operands are true, and false if either operand is false. The OR operator (||) returns true if either operand is true, and false if both operands are false. The NOT operator (!) negates the value of a boolean expression. If the expression is true, it returns false, and if it is false, it returns true.

## Shift Operators in Java

Shift operators are used to shift the bits of an integer value left or right. They are often used to perform arithmetic operations on integers more efficiently than using the standard arithmetic operators.

Operator | Name | Description | Example |
---|---|---|---|

<< | Shift left | The shift left operator moves the bits in a variable to the left by the number of spaces indicated by the value on the right. This effectively increases the variable by a factor of 2 raised to a certain power. | int x = 5;
x <<= 3; |

>> | Shift right | The shift right operator shifts the bits of the variable on the left by the number of positions specified by the value on the right. It effectively divides the variable by a power of 2. | int x = 5;
x >>= 3; |

>>> | Shift right zero fill | The shift right zero fill operator shifts the bits of the variable on the left by the number of positions specified by the value on the right, filling the empty positions with zeros. It effectively divides the variable by a power of 2. | int x = 5;
x >>>= 3; |

It’s important to note that the shift operators have a **lower precedence** than most other operators, so expressions involving them may need to be parenthesized to ensure that they are evaluated correctly.

### Examples

Here are some examples of how to use shift operators in Java:

// Left shift int x = 5; int y = x << 2; // y is now 20 // Right shift int x = 20; int y = x >> 2; // y is now 5 // Left shift with assignment int x = 5; x <<= 2; // x is now 20 // Right shift with assignment int x = 20; x >>= 2; // x is now 5

The left shift and assign operator (<<=) performs a left shift on the left operand by the number of positions specified by the right operand and then assigns the result back to the left operand. It is equivalent to writing “x = x << y”.

The right shift and assign operator (>>=) performs a right shift on the left operand by the number of positions specified by the right operand and then assigns the result back to the left operand. It is equivalent to writing “x = x >> y”.

## Relational Operators in Java

Relational operators are used to compare two values and return a boolean result based on the comparison. They are often used in conjunction with **if-else** statements and loops to control the flow of a program based on certain conditions.

Here is a table of relational operators in Java, along with their name, description, and example:

Operator | Name | Description | Example |
---|---|---|---|

== | Equal to | The equal to operator compares two values for equality and returns true if they are equal, and false if they are not. It does not perform type coercion, so the operands must be of the same type. | int x = 5; boolean y = (x == 5); |

!= | Not equal to | The not equal to operator compares two values for inequality and returns true if they are not equal, and false if they are. It does not perform type coercion, so the operands must be of the same type. | int x = 5; boolean y = (x != 3); |

> | Greater than | The greater than operator checks if the value on the left is larger than the value on the right. If it is, the operator returns true, and if it is not, it returns false. | int x = 5; boolean y = (x > 3); |

< | Less than | The less than operator checks if the value on the left is smaller than the value on the right. If it is, it returns true, and if it’s not, it returns false. | int x = 5; boolean y = (x < 3); |

>= | Greater than or equal to | The greater than or equal to operator checks whether one value is greater than or equal to another. If the value on the left is greater than or equal to the value on the right, it returns true. If not, it returns false. | int x = 5; boolean y = (x >= 3); |

<= | Less than or equal to | The less than or equal to operator checks if the value on the left is less than or equal to the value on the right. If it is, it returns true, and if it is not, it returns false. | int x = 5; boolean y = (x <= 3); |

Relational operators generally have lower priority than most other operators, so it may be necessary to enclose them in parentheses to guarantee they are correctly processed in an expression.

### Examples

Relational operators are often used to compare values in conditional statements, such as if-else statements or loops. For example, you could use the greater than operator to check if a variable is greater than a certain value:

int x = 5; if (x > 3) { // Do something }

Or you could use the not equal to operator to check if a variable is not equal to a certain value:

int x = 5; if (x != 3) { // Do something }

Relational operators are also used in boolean expressions, where they can be combined with logical operators such as AND and OR to create more complex conditions. For example, you could use the AND operator to check if a variable is greater than 3 and less than 10:

int x = 5; if (x > 3 && x < 10) { // Do something }

## Unary Operators in Java

Unary operators in Java are operators that operate on a **single operand**. They perform various operations on the operand, such as negating its value, incrementing or decrementing it, or inverting its boolean value.

Here is a table of unary operators in Java, along with their name, description, and example:

Operator | Name | Description | Example |
---|---|---|---|

+ | Unary plus | The unary plus operator indicates a positive value. It has no effect on the value of the operand, as all values in Java are already considered positive. It is used mostly to indicate a positive value explicitly. | int x = +5; |

– | Unary minus | The unary minus operator negates the value of the operand. It converts a positive value to a negative one, or vice versa. | int x = -5; |

++ | Increment | The increment operator increases the value of the operand by 1. It can be placed before (prefix) or after (postfix) the operand. | int x = 5; x++; |

— | Decrement | The decrement operator decreases the value of the operand by 1. It can be placed before (prefix) or after (postfix) the operand. | int x = 5; x–; |

! | Logical NOT | The logical NOT operator negates the value of a boolean expression. If the expression is true, it returns false, and if it is false, it returns true. | boolean x = !true; |

~ | Bitwise NOT | The bitwise NOT operator inverts the bits of an integer value. It changes 1s to 0s and 0s to 1s. | int x = ~5; |

(type) | Type casting | The type casting operator is used to explicitly convert the value of one type to another. The type in parentheses is the target type, and the value to be converted is placed before the operator. | int x = (int) 5.6; |

new | Object creation | The object creation operator is used to create a new instance of a class. It is followed by the name of the class and any necessary arguments for the class constructor. | String s = new String(“Hello”); |

Note that the increment and decrement operators, ++ and –, can be used as both unary and binary operators, depending on their placement and use. When used as a unary operator, they operate on a single operand and modify its value. When used as a binary operator, they take two operands and return a value.

### Examples

Here are some examples of how to use unary operators in Java:

// Unary plus int x = +5; // x is now 5 // Unary minus int x = -5; // x is now -5 // Increment int x = 5; x++; // x is now 6 // Decrement int x = 5; x--; // x is now 4 // Logical complement boolean x = true; boolean y = !x; // y is now false

The unary plus operator (+) does not have any effect on the operand, but it is often used to indicate that a value is positive.

The unary minus operator (-) negates the value of the operand.

The increment operator (++) increases the value of the operand by 1. It can be used as a prefix (++x) or a postfix (x++). When used as a prefix, it increments the operand and then returns the new value. When used as a postfix, it returns the original value and then increments the operand.

The decrement operator (–) decreases the value of the operand by 1. It can be used as a prefix (–x) or a postfix (x–). When used as a prefix, it decrements the operand and then returns the new value. When used as a postfix, it returns the original value and then decrements the operand.

The logical complement operator (!) negates the value of a boolean expression. If the expression is true, it returns false, and if it is false, it returns true.

### What is the difference between **Unary** and **Binary** Operators in Java

In Java, unary operators are operators that operate on a single operand, while binary operators are operators that operate on two operands.

Unary operators perform various tasks, such as negating a value, increasing or decreasing a value by 1, inverting bits, converting the value of one type to another, and creating a new instance of a class.

Binary operators, on the other hand, perform operations on two operands. Some examples of binary operators in Java include the arithmetic operators (+, -, *, /, %), the assignment operators (=, +=, -=, *=, /=, %=), the relational operators (>, <, >=, <=, ==, !=), and the logical operators (&&, ||). These operators perform more complex operations, such as performing arithmetic calculations or comparing values.

## Bitwise Operators in Java

Bitwise operators are used to perform bit-level operations on integer values, such as setting, clearing, or testing individual bits.

Operator | Name | Description | Example |
---|---|---|---|

& | Bitwise AND | The bitwise AND operator performs a logical AND operation on each bit of the operands. It returns 1 if both bits are 1, and 0 otherwise. | int x = 5 & 3; |

^ | Bitwise XOR | The bitwise XOR operator performs a logical XOR operation on each bit of the operands. It returns 1 if one of the bits is 1 and the other is 0, and 0 otherwise. | int x = 5 ^ 3; |

| | Bitwise OR | The bitwise OR operator performs a logical OR operation on each bit of the operands. It returns 1 if either of the bits is 1, and 0 otherwise. | int x = 5 | 3; |

~ | Bitwise NOT | The bitwise NOT operator inverts the bits of an integer value. It changes 1s to 0s and 0s to 1s. | int x = ~5; |

The bitwise operators have a lower precedence than most other operators, so expressions involving them may need to be parenthesized to ensure that they are evaluated correctly.

The results of executing logical operators in bitwise operators are the same as in logical ones, but Bitwise XOR is added:

A | B | A & B | A ^ B | A | B | ~A |
---|---|---|---|---|---|

false | false | false | false | false | true |

true | false | false | true | true | false |

false | true | false | true | true | true |

true | true | true | false | true | false |

### Examples

Here are some examples of how to use bitwise operators in Java:

// Bitwise AND int x = 5; // 5 is represented in binary as 101 int y = 3; // 3 is represented in binary as 011 int z = x & y; // z is 1, represented in binary as 001 // Bitwise OR int x = 5; // 5 is represented in binary as 101 int y = 3; // 3 is represented in binary as 011 int z = x | y; // z is 7, represented in binary as 111 // Bitwise XOR int x = 5; // 5 is represented in binary as 101 int y = 3; // 3 is represented in binary as 011 int z = x ^ y; // z is 6, represented in binary as 110 // Bitwise NOT int x = 5; // 5 is represented in binary as 101 int y = ~x; // y is -6, represented in binary as 11111111111111111111111111111010

## Ternary Operator In Java

The ternary operator in Java is also known as the conditional operator. It is a shorthand way to write an** if-else** statement and is used to choose one of two values based on a boolean condition. It can be particularly useful when you need to return a value based on a boolean condition, but don’t want to write an entire **if-else** block.

The ternary operator has the following syntax:

condition ? expression1 : expression2

If the condition is true, the operator returns the value of expression1. If the condition is false, it returns the value of expression2.

Here is a table of the ternary operator in Java, along with its name, description, and example:

Operator | Name | Description | Example |
---|---|---|---|

? : | Ternary | The ternary operator takes a boolean condition as its first operand, followed by a value to be returned if the condition is true, and a value to be returned if the condition is false. It is a shorthand way to write an if-else statement. | int x = (5 > 3) ? 5 : 3; // x will be assigned 5 |

The ternary operator has a lower precedence than most other operators, so expressions involving it may need to be parenthesized to ensure that they are evaluated correctly.

### Examples

Here is an example of how the ternary operator can be used as a shorthand way to write an if-else statement:

int x; if (5 > 3) { x = 5; } else { x = 3; }

This can be rewritten using the ternary operator like this:

int x = (5 > 3) ? 5 : 3;

Both of these examples assign the value 5 to the variable x if 5 is greater than 3, and the value 3 to x if 5 is not greater than 3.

## Java instanceof Operator

The instanceof operator in Java is a type comparison operator that allows you to determine whether an object is an instance of a particular class or interface. It has the following syntax:

object instanceof class

The instanceof operator checks if the object on the left is an instance of the class or interface on the right. If it is, the operator returns true. If it is not, it returns false.

### Examples

Here is an example of how to use the “instanceof” operator in Java:

String s = "Hello, world!"; if (s instanceof String) { System.out.println("s is a String"); }

In this example, the instanceof operator checks if the object s is an instance of the String class. Since s is indeed a String, the operator returns true and the message “s is a String” is printed to the console.

The instanceof operator is often used to check the type of an object before performing operations on it. For example, you might use it to ensure that an object is an instance of a particular class before calling a method on it:

Object obj = "Hello, world!"; if (obj instanceof String) { String s = (String) obj; int length = s.length(); // Do something with the length of the string }

In this example, the instanceof operator is used to check if obj is an instance of the String class. If it is, the object is cast to a String and the length of the string is determined using the length() method.

The instanceof operator is an important part of programming in Java, as it allows you to determine the type of an object and take appropriate action based on that type.

## Java Operator Precedence

Here is a table that lists the operators in Java by priority, category, and associativity:

Priority | Category | Operator | Associativity |
---|---|---|---|

1 | Postfix | ++, — | Left-to-right |

2 | Unary | +, -, ~, ! | Right-to-left |

3 | Multiplicative | *, /, % | Left-to-right |

4 | Additive | +, – | Left-to-right |

5 | Shift | <<, >>, >>> | Left-to-right |

6 | Relational | <, >, <=, >=, instanceof | Left-to-right |

7 | Equality | ==, != | Left-to-right |

8 | Bitwise AND | & | Left-to-right |

9 | Bitwise XOR | ^ | Left-to-right |

10 | Bitwise OR | | | Left-to-right |

11 | Logical AND | && | Left-to-right |

12 | Logical OR | || | Left-to-right |

13 | Conditional | ? : | Right-to-left |

14 | Assignment | =, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>= | Right-to-left |

This table shows the operator categories in order of priority, **with the highest priority operators listed first**. Within each category, the operators are listed in order of associativity, with left-to-right associativity listed before right-to-left associativity.

For example, the postfix ++ and — operators have a higher priority than the unary + and – operators, which means that they will be evaluated before the unary operators. Similarly, the multiplicative * and / operators have a higher priority than the additive + and – operators, which means that they will be evaluated before the additive operators.

It’s important to note that the priority and associativity of the operators can affect the order in which they are evaluated in an expression. It’s generally a good idea to use parentheses to clarify the intended order of evaluation in complex expressions.