# 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; `

+ : Addition
Adds together two values
Example: `x + y;`

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

Example: Click Here

## 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;`

Example: Click Here

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

Example: Click Here

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

### Logical Operators with their description:

Example: Click Here

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

### Following are the relational operators in Java:

Example: Click Here

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

Example: Click Here

## 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:

Example: Click Here

## 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 check Krishnamurthy Number

In this tutorial, we will learn about Krishnamurthy numbers and write a Krishnamurthy Number program in Java. We will write two programs for Krishnamurthy number …

### Java Program for ISBN Number

In this tutorial, we will learn about the ISBN (International Standard Book Number) and write a program to check for the ISBN Number in Java …

### C++ Memory Management

Memory management refers to the process of managing the computer memory while assigning the space to the program’s variable to improve the overall performance. Requirement …

### C++ Pointer to Pointer

As we know by now that a pointer stores the address of the pointed variable. But it is not the only use, pointer also stores …

### C++ Passing Pointers to Functions

A function is a user-defined block of codes that executes some specific task assigned to it invoked by its name. If there is an argument …

### C++ Null Pointer

We use a null pointer when we do not have the exact address to assign to a pointer. It is considered a good practice and …