# Java – Operator & Expression6 min read

Operators are used to perform operations on variables and values or to manipulate them. The operation is performed with two operand and operators to provide what action need to perform.

Example:

In the above example, 10 and 5 are operands and + is the operation performed between these two operands.

### Java provides numbers of operation and these are grouped into the following:

• Arithmetic Operators
• Assignment Operators
• Increment and Decrement Operators
• Logical Operators
• Relational (comparison) operators
• Bitwise Operators
• Ternary Operator

## 1. Arithmetic Operators:

They are used to perform basic arithmetic operations. They perform on primitive data types.
They are:

* : Multiplication
Multiplies two values
Example: `x * y;`

/ : Division
Divides one value from another
Example: `x / y;`

% : Modulo
Returns the division remainder
Example: `x % y; `

Example: `x + y;`

– : Subtraction
Subtracts one value from another
Example: `x - y;`

## 2. Assignment Operators:

The assignment operator is used to assigning a value to any variable. This operator assigns the value of the right-hand side of an operator to the left-hand side. Various assignment operator in Java:

These are the short version formed by combining the two operators.

For example Instead of writing, `int a = a+3`, we can write `a+= 5`.

• Assignment operator: ‘=’ This operator assigns the value on the right to the variable on the left.
`a = 20; B = 30; Ch = 'cha';`
• Add AND assignment operator: ‘+=’ This operator first adds the current value of the variable on the left to the value on the right and then assigns the result to the variable on the left.
`c+=7; a+=b;`
• Subtract AND assignment operator: ‘-=’ This operator first subtracts the current value of the variable on the left from the value on the right and then assigns the result to the variable on the left.
`c-=7; a-=b;`
• Multiply AND assignment operator: ‘*=’ This operator first multiplies the current value of the variable on the left to the value on the right and then assigns the result to the variable on the left.
`c*=7; a*=b;`
• Divide AND assignment operator: ‘/=’ This operator first divides the current value of the variable on the left by the value on the right and then assigns the result to the variable on the left.
`c/=7; a/=b;`
• Modulus AND assignment operator: ‘%=’ It takes modulus using two operands and assigns the result to the left operand.
`C %= A is equivalent to C = C % A ;`
• Right shift AND assignment operator: ‘>>=’ This operator is used for Right shift Operation.
`C >>= 2 ;`
• Left shift AND assignment operator: ‘<<=’ This operator is used for Left shift Operation.
`C <<= 2 ;`
• Bitwise AND assignment operator: ‘&=’ This operator is used for Bitwise AND Operations.
`C &= 2;`
• Bitwise exclusive OR and assignment operator: ‘^=’ This operator is used for Bitwise exclusive OR Operations.
`C ^= 2;`
• Bitwise inclusive OR and assignment operator: ‘|=’ This operator is used for Bitwise inclusive OR.
`C |= 2;`

## 3. Increment and Decrement Operators:

The ++ and the – – are Java’s increment and decrement operators. ++ is used to increase the value by 1 and — is used to decrease the value by 1. There are two kinds of Increment and Decrement Operators.

They are:

• Post-Increment or Post-Decrement:
First, the value is used for operation and then incremented or decremented. Represented like a++ or a–.
• Pre-Increment Pre-Decrement:
Here First the value is incremented or decremented then used for the operation. Represented like ++a or –a.

## 4. Logical Operators:

Logical Operators are used in conditional statements and loops for evaluating a condition with binary values. All of the binary logical operators combine two boolean values that are true and false to form a result value.

## 5. Relational (comparison) operators:

Comparison operators are used to comparing two values and return boolean results. These operators are used to check for relations like equality, greater than, less than between two values. They are used with a loop statement and also with if-else statements.

## 6. Bitwise operators:

Bitwise operator works on bits and performs a bit-by-bit operation. There are six Bitwise Operators and can be applied to the integer types, long, int, short, char, and byte.

• Bitwise AND: ‘&’
Binary AND Operator copies a bit to the result if it exists in both operands.
• Bitwise OR: ‘|’
Binary OR Operator copies a bit if it exists in either operand.
• Bitwise XOR: ‘^’
Binary XOR Operator copies the bit if it is set in one operand but not both.
• Binary Left Shift: ‘<<’
The left operands value is moved left by the number of bits specified by the right operand.
• Binary Right Shift: ‘>>’
The left operand’s value is moved right by the number of bits specified by the right operand.
• Bitwise complement: ‘~’
Binary Ones Complement Operator is unary and has the effect of ‘flipping’ bits.
• zero-fill right shift: ‘>>>’
The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros.

## 7. Ternary operators:

Ternary Operator is also known as the Conditional operator. It is used to evaluate Boolean expressions. We can say that it is a short version of the if-else statement. But here instead of operating on to operand, it operates on three operands, and therefore the name ternary.

Syntax:

`variable num1 = (expression) ? value if true : value if false;`

## What are Precedence and Associativity?

Precedence and Associativity are the rules that are used to determine the operators with the highest priority in evaluating an equation that contains different operations.

For example:

In this example, x is assigned as 20, not 30 that’s because operator * has higher precedence than +, and hence it first gets multiplied (5*2) and then the multiplied value is added to 10.

### Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

### C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

### C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

### C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

### C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

### Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …