In this post, you will learn to:

- Describe an operator and explain its purpose.
- Identify and explain the use of Assignment, Arithmetic, and Unary operators.
- Identify and explain the use of Equality, Relational, and Conditional operators.
- Identify and explain the use of Bitwise and Bit Shift operators.
- Explain operator precedence.
- Explain operator associativity.

## Purpose

All programming languages provide some mechanism for performing various operations on the data stored in variables. The simplest form of operations involves arithmetic (like adding, dividing, or comparing between two or more variables). A set of symbols is used to indicate the kind of operation to be performed on data. These symbols are called operators.

Consider the expression:

The + symbol in the statement is called the operator and the operation performed is addition. This operation is performed on the two variables X and Y, which are called operands. The combination of both the operator and the operands, Z = X + Y, is known as an Expression.

Java provides several categories of operators and they are as follows:

- Arithmetic
- Relational
- Logical
- Assignment
- Bitwise
- Bit Shift

## Assignment Operator

The basic assignment operator is a single equal to sign, ‘=’. This operator is used to assign the value on its right to the operand on its left. Assigning values to more than one variable can be done at a time. In other words, it allows you to create a chain of assignments.

Consider the following statements:

int balance = 3456; char gender="M";

In the statements, the values 3456 and M are assigned to the variables namely, balance and gender. In addition to the basic assignment operator, there are combined operators that allow you to use a value in an expression, and then set its value to the result of that expression.

The second statement stores the value 8, the meaning of the statement is that X = X + 5. The following code demonstrates the use of different assignment operators.

Code Snippet:

x = 10; // Assigns the value 10 to variable x x += 5; // Increments the value of x by 5 x -= 5; // Decrements the value of x by 5 x *= 5; // Multiplies the value of x by 5 x /= 2; // Divides the value of x by 2 x %= 2; // Divides the value of x by 2 and the remainder is returned

## Arithmetic Operator

Arithmetic operators manipulate numeric data and perform common arithmetic operations on the data.Operands of the arithmetic operators must be of numeric type. Boolean operands cannot be used, but character operands are allowed. The operators mentioned here are binary in nature: that is, these operate on two operands, such as X+Y. Here, + is a binary operator operating on X and Y.

The table below lists the arithmetic operators.

Operator | Description |
---|---|

+ | Addition – Returns the sum of the operands. |

– | Subtraction – Returns the difference of two operands. |

* | Multiplication – Returns the product of operands. |

/ | Division – Returns the result of division operation. |

% | Remainder – Returns the remainder from a division operation. |

The following code demonstrates the arithmetic operators.

Code Snippet:

x = 2 + 3; // Returns 5 y = 8 – 5; // Returns 3 x = 5 * 2; // Returns 10 x = 5/2; // Returns 2 y = 10 % 3; // Returns 1

## Unary Operator

Unary operators require only one operand. They perform various operations, such as incrementing/ decrementing the value of a variable by 1, negating an expression, or inverting the value of a boolean.

The table below lists the unary operators.

Operator | Description |
---|---|

+ | Unary plus – Indicates a positive value. |

– | Unary minus – Negates an expression. |

++ | Increment operator – Increments value of a variable by 1. |

— | Decrement operator – Decrements value of a variable by 1. |

! | Logical complement operator – Inverts a boolean value. |

The ++ and — operators can be applied before (prefix) or after (postfix) the operand. The statements ++variable and variable++ both result in incrementing the variable value by 1. The only difference is that the prefix version (++variable) will increment the value before evaluating, whereas the postfix version (variable++) will first evaluate and then increment the original value.

The following code demonstrates the unary operators.

Code Snippet:

int i=5; int j=i++; // i=6, j=5 int k=++i; //i=6,k=6 i = - i ; //now i is -6 boolean result = false; //result is false result = !result; //now result is true

## Equality and Relational Operators

Equality and relational operators test the relationship between two operands. An expression involving relational operators always evaluates to a boolean value (that is, either true or false).

The table below lists the various relational operators.

Operator | Description |
---|---|

== | Equal to – Checks equality of two numbers. |

!= | Not Equal to – Checks inequality of two values. |

> | Greater than – Checks if value on left is greater than the value on the right. |

Less than – Checks if the value on the left is lesser than the value on the right. | |

>= | Greater than or equal to – Checks if the value on the left is greater than or equal to the value on the right. |

Less than or equal to – Checks if the value on the left is less than or equal to the value on the left. |

The following code demonstrates the relational operators.

Code Snippet:

int value1 = 10; int value2 = 20; value1 == value2; //returns false value1 != value2; //returns true value1 > value2; //returns false value1 < value2; //returns true value1 <= value2; //returns true

## Conditional Operators

Conditional operators (&& and ||) work on two boolean expressions. These operators exhibit short-circuiting behavior, which means that the second operand is evaluated only if needed.

The table below lists the two conditional operators.

Operator | Description |
---|---|

&& | Conditional AND – Returns true only if both the expressions are true. |

|| | Conditional OR – Returns true if either expression is true or both the expressions are true. |

Ternary operator (?:) is another type of conditional operator; it accepts three operands. The syntax for the usage of ternary operator is as follows:

expression1 ? expression2 : expression3

where,**expression1**: is a boolean condition that returns a true or false value**expression2**: is the value returned if expression1 evaluates true**expression3**: is the value returned if expression1 evaluates false.

The following code demonstrates the conditional operators.

Code Snippet:

int first = 10; int second = 20; (first == 30) && (second == 20 ); //returns false (first == 30) || (second == 20 ); //returns true (second > first) ? "second number is greater" : "first number greater or equal"; //returns "second number is greater"

## Bitwise and Bit Shift Operators

Bitwise operators work on binary representations of data. These operators are used to change individual bits in an operand. Bitwise shift operators are used to move all the bits in the operand left or right a given number of times.

The table below lists the various bitwise operators.

Operator | Description |
---|---|

& | Bitwise AND – compares two bits and generates a result of 1 if both bits are 1; otherwise, it returns 0. |

| | Bitwise OR – compares two bits and generates a result of 1 if the bits are complementary; otherwise, it returns 0. |

^ | Exclusive OR – compares two bits and generates a result of 1 if either or both bits are 1; otherwise, it returns 0. |

~ | Complement operator – used to invert all of the bits of the operand. |

>> | Shift Right operator – Moves all bits in a number to the right by one position retaining the sign of negative numbers. |

Shift Left operator – Moves all the bits in a number to the left by the specified position. |

The following code demonstrates the bitwise operators.

Code Snippet:

int x = 23; int y = 12; //23 = 10111 , 12 = 01100 (x & y) // returns 4 , i.e, 4 = 00100 (x | y) //returns 31, i.e 31 = 11111 (x ^ y) //returns 27, i.e 31 = 11011 int a = 43; int b = 1; //43 = 010011 , 1 = 000001 (a >> b) // returns 21 , i.e, 21 = 0010101 (a << b) //returns 86 , i.e, 86 = 1010110

## Operator Precedence

Expressions that are written generally consist of several operators. The rules of precedence decide the order in which each operator is evaluated in any given expression.

The table below shows the order of precedence of operators, from highest to lowest in which operators are evaluated in Java.

Order | Operator |
---|---|

1. | Parentheses like ( ). |

2. | Unary Operators like +, -, ++, –, ~, !. |

3. | Arithmetic and Bitwise Shift operators like *, /, %, +, -, >>, |

4. | Relational Operators like >, >=, |

5. | Conditional and Bitwise Operators like &, ^, |, &&, ||. |

6. | Conditional and Assignment Operators like ?:, =, *=, /=, +=, and -=. |

Parentheses are used to change the order in which an expression is evaluated. Any part of an expression enclosed in parentheses is evaluated first. The following code demonstrates the precedence of operators in an expression.

Code Snippet:

2*3+4/2 > 3 && 3<5 || 10<9

The evaluation is as shown:

1. (2*3+4/2) > 3 && 3<5 || 10<9

First the arithmetic operators are evaluated.

2.((2*3)+(4/2)) > 3 && 3<5 || 10<9

Division and Multiplication are evaluated before addition and subtraction.

3. (6+2) >3 && 3<5 || 10<9

4. (8 >3) && [3<5] || [10<9]

Next to be evaluated are the relational operators all of which have the same precedence. These are therefore evaluated from left to right.

5. (True && True) || False

The last to be evaluated are the logical operators. && takes precedence over ||.

6. True || False

7. True

## Operator Associativity

When two operators with the same precedence appear in an expression, the expression is evaluated,according to its associativity. For example, in Java the – operator has left-associativity and x – y – z is interpreted as (x – y) – z, and = has right-associativity and x = y = z is interpreted as x = (y = z).

The table below shows the Java operators and their associativity.

Operator | Description | Associativity |
---|---|---|

(), ++, — | Parentheses, post increment/decrement | Left to right |

++, –, +, -, !,~ | Pre increment/ decrement unary plus, unary minus logical NOT, bitwise NOT | Right to left |

*, /, %, +, – | Multiplicative and Additive | Left to right |

> | Bitwise shift | Left to right |

=, | Relational and Equality operators | Left to right |

&, ^, | | Bitwise AND, XOR, OR | Left to right |

&&, || | Conditional AND, OR | Left to right |

?: | Conditional operator (Ternary) | Right to left |

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

The following code demonstrates the associativity of operators in an expression.

Code Snippet:

Consider the following expression:

1. According to the rules of operator precedence, the ‘*’ has higher precedence than any other operator in the equation. Since 7-1 is enclosed in parenthesis, it is evaluated first.

2. Next, ‘*’ is the operator with the highest precedence. Since there are no more parentheses, it is evaluated according to the rules.

3. As ‘+’ and ‘-‘ have the same precedence, the left associativity works out.

4. Finally, the expression is evaluated from left to right.

The result is -14.

## Stay connected