# Operators in Java

## Introduction

Operators allow us to perform various mathematical and logical functions, like adding and comparing numbers. Each operator in an expression is evaluated in a predetermined order, known as operator precedence.

## Types

• Arithmetic operator
• Assignment operator
• Comparison operator
• Logical operator
• Unary operator
• Bitwise operator
• Shift operator
• Ternary operator

## Arithmetic operator

This is an operator used to perform arithmetic operations on operands. Addition is used to add two or more numbers, and subtract two or more numbers. Multiplication, division and modulus operation can also be performed.

## Assignment Operator

The assignment operator is categorized into two operators, the simple assignment operator, and the complex assignment operator. The simple assignment operator is used to assign a value to a variable. The complex assignment operator is combined with the simple assignment operator and it is the arithmetic assignment operator. They are shorthand for their equivalent long forms.+= - used to add two numbers and assign the result to a variable ._= - used to subtract two numbers and assign the result to a variable.

## Comparison Operator

A comparison operator is used to compare two values and perform an action on the basis of the result of that comparison. The expression results in a boolean value, true or false. Greater than - is used to check whether the value of the left operand is greater than the value of the right operand. The less than or equal to - used to check whether the value of the operand is less than or equal to the value of the right operand.

Syntax

operator1 instanceof operator2 .

## Logical Operator

Logical operators are used to evaluate operands and return a boolean value.

Logical And - used to compare two boolean expressions and returns true if both the boolean expressions are true.

Logical Or - used to compare two boolean expressions and return false if both the boolean expressions are false, or else it returns true if any one of the boolean expression is true. A boolean expression is an expression that results in a boolean value - true or false.

## Unary Operator

An operator that requires only one operand is called a unary operator. The unary plus operator indicates a positive value and the unary minus operator indicates a negative value.

Prefix form

In prefix form, the operand precedes the operand. The value is incremented or decremented before it is assigned to the operand.

n = 5; m = ++n;

Postfix form

In the postfix form, the operator follows the operand. The value is incremented or decremented after it is assigned to the operand.

n = 5; m = n++;

## Bitwise Operator

This is used for the manipulation of data at the bit level. The bitwise operator operates on the individual bits of their operands. The operands are various types int, short, long, char, and byte.

It is categorized into four functions: Bitwise AND, Bitwise OR, Bitwise NOT, Bitwise XOR.

Bitwise AND - It performs an and operation on two operands. The And operator produces 1 if both the bits are 1 or else 0.

Bitwise OR - It performs OR operation on two operands.

Bitwise NOT - It is a unary operator and performs the NOT operation on each bit of binary number. It is a bitwise complement.

Bitwise XOR - It performs the XOR operation in two operands. It is applied to two bits.

## Shift Operator

It is used to shift the bits of its operand either to the left or to the right.

Types: Right shift operator, left shift operator, Unsigned right shift operator.

Right shift operator: It shifts all the bits of a binary number in the right direction.

## Ternary operator

It is used to evaluate an expression. It works on logical expressions and two operands. It returns one of two operands.