So it’s time to cover **Operators in java**. So far We have covered Below topics. If you are new to java and havn’t gone through these, then I highly recommend you to go through these topics before jumping to this one.

Operators are a symbol that defines an operation which happens on one or more operands. For example, in sum of numbers “+” is an operator

In java we have multiple operators. Let’s Discuss them one by one

Table of Contents

## Arithmatic Operators in java

Operators which are used for arithmetic operations like addition, substraction etc are called Arithmatic Operators. Below are arithmetic operators in java

- + This is used for addition of numbers. It needs two operands eg: a + b. Thi is also used to concatenate two strings.
- – This is used for substraction of number, this also needs two operands. If applied on single operand of any type of number then that signifies that it’s a negative number
- * is used to multiply two numbers
- / is used to devide two number
- % denotes modulus, it is called mod. If the expression is a % b then we read it a mod b. result of this operator is remainder. eg: 5 % 2 = 1
- = is used for assignment
- ++ used to increament number by one
- — used to dresease the number by 1

## Logical Operators in Java

Now, let’s understand **what are logical operators in java**? Logical operators are used for binary operations. Below are logical operators in java

- || denotes OR operation. It needs two boolean operands
- && denotes AND operation. It also needs two boolean operands
- & is
**bitwise AND**. Unlike && if & is used then AND operation is applied on every bit of operand one with corresponding bit of operand 2 - | works just like & but it used for bitwise OR operation
- ^ is used for XOR operation

## Conditional Operators in java

Let’s find out **what is conditional operator in java**? Conditional operators are used to evaluate an expression and result a boolean. Below are Conditional operators in java

- > denotes greater than. It needs two operands to work eg: 5>4 returns TRUE, since 5 is greater than 4
- < denotes less than. It also needs two operands to work eg: 5>4 returns FALSE, since 5 is not lesser than 4
- >= greater than and equal to. It checks for two condition, either greater than or equal to
- <= less than and equal to. It checks for two condition, either less than or equal to
- ! means NOT, It toggles the boolean value. eg !(5 >6) returns TRUE, since 5 is not greater than 6 so that evaluates to FALSE, but since we have ! infront of the expression hence that will toggle FALSE and make that TRUE.

## Shift Operators in java

Shift Operators shifts the value by given number of bits. Below are Shift operators in java

>> is called **Right Shift Operator**

<< is called **Left shift operator**

### Understand Right Shift Operator

2 >> 1 = 1

**explation:**

2 in binary is 10

now if we shift it by one bit

10 -> 1

hence answer will be 1

If I still fail to explain the shift operators then please read more here

## Ternary Operator in java

Ternary operator is used to evaluate if, else in one instruction. Below is format of ternary oeprator

<expression> ? <result if expression is true> : <result if expression is false>

Take a look at example of ternary operator

6>8 ? “6 is greater than 8” : “6 is not greater than 8”

in the above example result will be “6 is not greater than 8”, since 6>8 is false

So that’s it about Operators in java. I’m listing below few of the question people generally get confused with.

## Frequently Asked questions:

## What is ternary operator in java?

Ternary operators are used as substitute of if and else. It shortens if and else in one expression. Read above the explanation on ternary operator.

## What is operator overloading in java

Java do not support operator overloading. Please do not be confused with “+” operator since that adds number as well as concatenate strings. That is inbuilt behaviour. Java doesn’t allow us to overload an operator. So, if someone asks **does java support operator overloading**? The answer is NO.

## What is bitwise operator in java

| and & are bitwise operators in java. The performs OR and AND operation respectively on corresponding bits of both operands.

Let’s consider 4 | 5

4 in binary is 1 0 0

5 in binary is 1 0 1

so 4 | 5 would be or of 4 and 5 bitwise.

1 | 0 | 0 |

OR | OR | OR |

1 | 0 | 0 |

= | = | = |

1 | 0 | 1 |

so 4 | 5 = 5

## What is unary operator in java?

Unirary operators operate on single operand. In java, we see below unirary operands

- ! is used for NOT
- – marks a number as negative
- ++ used to increament number by one
- — used to dresease the number by 1

## What is new operator in java

new oeprator in java is used with a constructor to create an object in java.

I have tried my best to cover everything about operators in java. But still if I miss any or If you have any question, please feel free to ask in comment.

## 2 thoughts on “Operators in java – A complete Java Operator guide”