Using Operators in java, we can perform the operation on operands, variables. When we define variables we can perform operations on them using operators. For example if there are two variables A and B of integer type then we can make sum of these two variables or subtraction or multiplication or division etc. these all kinds of operation are performed on operands.

In java we have different kinds of operators some of them are listed below that will help you to understand operators deeply.

**Types Of Operators:**

- Arithmetic Operators.
- Relational Operators.
- Bitwise Operators.
- Logical Operators.
- Assignment Operators.
- Others Operators.

Now we go through each of these for more understanding.

### 1. Arithmetic Operators

Any kind of mathematical operation can be performed using Arithmetic operators. Such as addition, subtraction, manipulation and division. In java following are Arithmetic Operators can be used to achieve mathematic calculations.

A. Addition (+).

B. Subtraction (-).

C. Multiplication (*).

D. Division (/).

E. Modulus (%).

F. Increment (++).

G. Decrement (--).

In Java there are two types of Arithmetic Operators.

- Unary Operator
- Binary Operator

### Unary Operators Are

Unary operator has single operand.

Below listed Are unary arithmetic operator of Java:

- + ( Plus )
- - ( Minus )
- ++ (Increment Operator)
- -- (Decrement Operator)

### + (Unary Plus )

This operator represents the positive value of arithmetic operand.

Syntax

+ [Operand]

Example:

Int A; A=+10;

Here in the above example a variable has a positive value 10.

### - (Minus)

This operator represents the negative value of arithmetic operand.

Syntax:

-[Operand]

Example:

Int A; A=-10;

Here in the above example a variable has a negative value which is -10.

### Increment Operator /Decrement Operator

These unary operators are used to increment value of the operand with one or decrement value of the operand with one.

There are mainly two types of Increment and decrement operators:

**Pre-Increment And
Pre-Decrement Operator (++,--)**

Pre-Increment and pre-decrement operator is used before the operand. like

++ [operand];

-- [operand];

These operator first increment or decrement operand value.

Example Of Increment and Decrement operator is given below:

**Listing
1:TestArithmaticOperator.java**

public class TestArithmaticOperator { public static void main(String[] argues) { int i=10; int j=++i; //Here i increment by one Than Assingn to j. System.out.println("Value Of i = "+ i +"\nValue Of j = "+j); j=--i; //Here i decrement by one Than Assingn to j. System.out.println("Value Of i = "+ i +"\nValue Of j = "+j); } }

**Output Listing 1**:

Value Of i = 11

Value Of j = 11

Value Of i = 10

Value Of j = 10

In the above example, we can clearly understand the actual concept of pre-increment and decrement operator. First “i” is incremented by one and then it assigned to j, same things happen in decrement operation.

### Post-Increment And Post-Decrement Operator (++,--)

Post -increment and post -decrement operator is used after the operand. We can say right side of the operand.

[operand]++;

[operand]--;

This operator increase or decrease the value of operand Last.

Example of Increment and decrement operator is given below:

**Listing 2:
TestArithmaticOperator.java**

public class TestArithmaticOperator { public static void main(String[] args) { int i=10; int j=i++; //Here i Assingn to j Than increment by one. System.out.println("Value Of i = " + i +"\nValue Of j = "+j); j=i--; //Here i Assingn to j Than decrement by one. System.out.println("Value Of i = " + i +"\nValue Of j = "+j); } }

**Output
Listing 2: TestArithmaticOperator.java**

Value Of i = 11

Value Of j = 10

Value Of i = 10

Value Of j = 11

Above example simply demonstrate the use of post increment and decrement operators.

### Binary Arithmetic Operator

In binary arithmetic operator, operator has two operands.

Syntax of binary operator

[operand] [operator] [operand];

**Binary Arithmetic Operators are**:

- + ( plus )
- - (minus)
- * (Multiplication)
- / (Division)
- %(Modulus)

### + (Binary Plus)

This operator is used for Addition of two variables or operand. We can combine two strings using + (plus) operator in java

**Syntax**:

[operand] + [operand]

### - (Binary minus)

This operator is used for subtraction of two variables or operand.

**Syntax**:

[operand] - [operand];

### * (Multiplication)

This operator is used for multiplication of two variables or operands.

**Syntax**:

[operand] * [operand];

### / (Division)

This operator is used for Division of two variables or operands.

**Syntax**:

[operand] / [operand];

### % (Modulus)

This operator is used to get Reminder.

**Syntax**:

[operand] % [operand]

**Example of Binary Operator:**

**Listing 3: TestArithmaticOperator.java**

public class TestArithmaticOperator { public static void main(String[] args) { float oparandOne=10; floatoparandTwo=20; System.out.println("Addition of Two oparands: (OparandOne + OparandTwo): " + (oparandOne+oparandTwo)); System.out.println("\nSubstraction of Two oparands: (OparandOne - OparandTwo): " + (oparandOne-oparandTwo)); System.out.println("\nMultiplication of Two oparands: (OparandOne * OparandTwo): " + (oparandOne*oparandTwo)); System.out.println("\nDivision of Two oparands: (OparandOne // OparandTwo): " + (oparandOne/oparandTwo)); System.out.println("\nModulus of Two oparands: (OparandOne % OparandTwo): " + (oparandOne%oparandTwo)); } }

**Output Listing 3:TestArithmaticOperator.java**

Addition of Two oparands: (OparandOne + OparandTwo): 30.0

Substraction of Two oparands: (OparandOne - OparandTwo): -10.0

Multiplication of Two oparands: (OparandOne * OparandTwo): 200.0

Division of Two oparands: (OparandOne // OparandTwo): 0.5

Modulus of Two oparands: (OparandOne % OparandTwo): 10.0

Above listed example shows common arithmetic operations that we can perform on variables.

Here we have seen common arithmetic operators. This operator must have a numeric type of operands. It will not support Boolean type operands. While it supports char type operands, because char type is actually, a subset of integer in Java.

### 2. Relational Operators

The relationship between two operands in java can be determined using relational operators. Relational operators determine equality between given operands and also ordering between operands. Following are Relational operators in Java.

A. Equal To (==).

B. Not Equal To (!=).

C. Greater Than (>).

D. Less Than (<).

E. Greater Than or Equal To (>=).

F. Less Than or Equal To (<=).

Above listed are relational operators that define a relation between operands. The output that we get from these operators is Boolean, that means it output is true or false state.

These types of operators are most widely used in if statement that is control statement in Java and looping statement.

**Syntax of Relational operator:**

[Operand] [Relational Operator] [Operand];

**Example
Of Relational Operator: **

**Listing 4: **TestRelationalOperator.java

/* In this Example we Check the Who win The Game * Using Relational Operator.*/ public class TestRelationalOperator { intplayerOne; intplayerTwo; intrematchFlag; publicTestRelationalOperator() { playerOne=0; playerTwo=0; rematchFlag=0; } public void setPlayerOneValue(intplayerOne) { this.playerOne=playerOne; } public void setPlayerTwoValue(intplayerTwo) { this.playerTwo=playerTwo; } publicintgetPlayerOneValue() { returnplayerOne; } publicintgetPlayerTwoValue() { returnplayerTwo; } public void checkWinner() { if(playerOne == playerTwo) { System.out.println("No One Wins Game is Tie Score is: " + playerOne); rematchFlag=1; } if(playerOne<playerTwo) { System.out.println("playerTwo Wins and Score is: " + playerTwo); rematchFlag=0; } if(playerOne>playerTwo) { System.out.println("PlayerOne Wins and Score is: " + playerTwo); rematchFlag=0; } if(rematchFlag != 0) { System.out.println("Rematch the Game"); } } public static void main(String[] args) { TestRelationalOperator game=new TestRelationalOperator(); System.out.println("\n\nGame 1"); game.setPlayerOneValue(20); game.setPlayerTwoValue(20); game.checkWinner(); System.out.println("\n\nGame 2"); game.setPlayerOneValue(24); game.setPlayerTwoValue(18); game.checkWinner(); System.out.println("\n\nGame 3"); game.setPlayerOneValue(14); game.setPlayerTwoValue(28); game.checkWinner(); } }

**Output Testing
4**:

TestRelationalOperator .java

Game 1

No One Wins Game is Tie Score is: 20

Rematch the Game

Game 2

PlayerOne Wins and Score is: 18

Game 3

playerTwo Wins and Score is: 28

Above example illustrate the use of Relation operator as it uses equality and ordering Operators.

Equality of two operands can be determined using two equal sign as single equal sign indicates assignment. So these things should be kept in mind about equality operators. Equality type and non equality type operators can have any type of operands. It supports all integer type, floating type, character type etc. comparison between two variable or we can say operand can be performed using equality and non equality operators. While less than or greater than the type of operators are used for ordering between two operands and they not support all the data types as it only supports numeric types of data types. Like integer float char etc. reason to support char is it is a subset of integer so partially it’s like numeric data type.

### 3. Bitwise Operators

Bitwise
operators operate upon the bit of its given operands. We can apply bitwise
operators to all the integer types variable such as **short, int, long, byte, char.**java has many bitwise operators that
are listed below:

A. Bitwise AND (&).

B. Bitwise OR (|).

C. Bitwise NOT (~).

D. Bitwise Exclusive OR (^).

E. Shift Left (<<).

F. Shift Right (>>).

G. Shift Right zero fill (>>>).

In java, all integer types are signed integer. Sign integer indicates that the variable can have either positive or negative value. All variable that have integer type can be represent using bits.

### Bitwise AND:

Bitwise AND operator output 1 bit if both operands bit have 1 value else in any case it will output 0.

For an Example:

0101

&1001

= 0001

Looking upon above example, you will get idea about bitwise And Operator. It produces 1 where both operands bit have 1 else in all cases it produce 0.

### Bitwise OR:

Bitwise OR operator output 1 bit if any of operands bit have 1 value in all other cases it will output 0.

For an Example:

0101

**|** 1001

= 1101

We can easily get idea about bitwise OR operator as it produce 1 where each of operands bit value is 1. Otherwise it produce 0

### .Bitwise NOT:

Bitwise NOT operator inverts bit
of it’s given operand. As its complements bits, it is also known as **bitwise complement.**

For an example:

~ 0101 value become

1010 after performing Bitwise NOT operation on It.

### Bitwise XOR:

Exclusive OR type of Bitwise operator is similar to OR operator but with little different. As Bitwise XOR (^) check bit of both operands and output 1 if only one operand’s bit have value 1 else in all cases it will return 0.

For an Example:

1100

**^ **0100

=1000.

Here in above example bitwise Exclusive OR operator produce 1 where only one operand has value 1 else it will produce 0.

### Bitwise Left Shift:

Left shift is used to shift bit as per user requirement in left direction.

Common syntax of left Shift Bitwise Operator is:

**Syntax:**

variable<< number of shift ;

here variable is the name of your variable where you want to perform shift operation and number of shift is any integer number which indicates the number of position to left shift in the given variable.

### Bitwise Right Shift:

Right shift is used to shift bit as per user requirement in Right direction.

**Syntax:**

Variable>>number of shift ;

here variable is the name of your variable where you want to perform shift operation and number of shift is any integer number which indicates the number of position to right shift in the given variable.

**Example
OF Bitwise Operator:**

**Listing 5:
TestBitwiseOperator.java**

public class TestBetwiseOperator { int var1; int var2; publicTestBetwiseOperator() { var1=0; var2=0; } publicint getVar1() { return var1; } publicint getVar2() { return var2; } public void setVar1(int var1) { this.var1=var1; } public void setVar2(int var2) { this.var2=var2; } public static void main(String[] args) { TestBetwiseOperatortestBetwiseOperator=new TestBetwiseOperator(); testBetwiseOperator.setVar1(2); testBetwiseOperator.setVar2(5); /* Here Betwise AND (&) return 0 because 2 & 5 * 00000010 (2) * & 00000101 (5) * -------- * 00000000 (0) * *it return Zero(0) */ System.out.println("Betwise And (&):"+ (testBetwiseOperator.getVar1() & testBetwiseOperator.getVar2())); /* Here Betwise OR (|) return 0 because 2 & 5 * 00000010 (2) * | 00000101 (5) * -------- * 00000111 (7) * *it return 7 */ System.out.println("Betwise Or (|):"+ (testBetwiseOperator.getVar1() | testBetwiseOperator.getVar2())); /* Here Betwise NOT operator Return A complement Of Value * ~ 00000010 * 11111101 */ System.out.println("Betwise NOT (~):"+ (~testBetwiseOperator.getVar1())); /* Here Bitwise XOR operator Return * 00000010 (2) * ^ 00000101 (5) * -------- * 00000111 (7) */ System.out.println("Betwise XOR (^):"+ (testBetwiseOperator.getVar1() ^ testBetwiseOperator.getVar2())); /* Here Bitwise LEFT SHIFT (<<) operator Return * 2<<5; * 00000010 << 5 * 01000000 (64) * it return 64. */ System.out.println("Betwise LEFT SHIFT (<<):"+ (testBetwiseOperator.getVar1() << testBetwiseOperator.getVar2())); testBetwiseOperator.setVar1(64); /* Here Bitwise RIGHT SHIFT (>>) operator Return * 64>>5; * 01000000 >> 5 * 00000010 (0) * it return 2. */ System.out.println("Betwise RIGHT SHIFT (>>):"+ (testBetwiseOperator.getVar1() >> testBetwiseOperator.getVar2())); } }

**Output Listing 5: TestBitwiseOperator.java**

Betwise And (&):0

Betwise Or (|):7

Betwise NOT (~):-3

Betwise XOR (^):7

Betwise LEFT SHIFT (<<):64

Betwise RIGHT SHIFT (>>):2

Above example demonstrate some common bitwise operator.

### 4. Logical Operators

Logical Operators in java can only operates on Boolean value. And the result is true or false.

Some of Logical Operators in Java are.

A. Logical AND (&).

B. Logical OR (|).

C. Logical XOR (^).

D. Short –circuit AND (&&).

E. Short –circuit OR (||).

F. Logical Unary NOT (!).

Logical AND, logical OR and Logical Not are performed in the same way like Bitwise operator but difference between them is Logical operator is performed upon Boolean value while Bitwise operator performed upon bits. So there is different in operands. And also logical operate produce output in Boolean while bitwise operator’s output depends on bits.

Here Logical Unary NOT operator Invert the value (state of variable).

If variable value is true and we perform logical not operation it then it will be false.

**Example of
Logical operator:**

**Listing
6: TestLogicleOperator.java **

public class TestLogicleOperator { publicTestLogicleOperator() { } public static void main(String args[]) { int Value1=10; int Value2=20; /* Here in the Logicle AND Operator (&&) * All Expression Must be True then it excute true part * otherwise it excute false part */ if( Value1 >= 0 && Value2 <= 0) { System.out.println("Both Condition True"); } else { System.out.println("One of the or Both are False"); } /* Here in the Logicle OR Operator (||) * Any Expression is True then it excute true part. * if Expression is False Then and only Then it excute false part ***/ if( Value1 >= 0 || Value2 >= 0) { System.out.println("Any one Condition True"); } else { System.out.println("All condition are False"); } /* Here is Logicle NOT Operator (!)*/ if(!(Value1==0)) { System.out.println("Logicle Not operator"); } } }

**Output Listing
6**:

TestLogicleOperator.java

One of the or Both are False

Any one Condition True

Logicle Not operator

Above example demonstrate the use of Logical AND, Logical OR and Logical NOT operator.

### 5. Assignment Operators.

We all are aware about
assignment operators. One of the most favorite assignment operator that we
commonly use is **= **. Assignment
operator is used to assign value to the variables, assign memory to object.
There are so many areas where we regularly use assignment operators. Some of
Assignment operators that are used in java are given below.

A. Assignment operator (=).

B. Addition Assignment (+=).

C. Subtraction Assignment (-=).

D. Multiplication Assignment (*=).

E. Division Assignment (/=).

F. Modulus Assignment (%=).

G. Bitwise AND Assignment (&=).

H. Bitwise OR Assignment (|=).

I. Bitwise Exclusive OR Assignment (^=).

J. Shift Left Assignment (>>=).

K. Shift right zero fill Assignment (>>>=).

L. Shift Left Assignment (<<=).

Above Listed are different assignment operators that are used for assigning value to the variable in different form.

**Example
of Assignment Operator:**

**Listing
7: AssignmentOperatorExample.java**

public class AssignmentOperatorExample { public static void main (String[] args) { int first, second, third; first=5; //simple assignment second= 10; second += first; // Addition Assignment System.out.println("Second Value is:- "+ second); second *= first; //Multiplication Assignment. System.out.println("After Multiplication Second Value is:- "+ second); }

Output
Listing 7:**AssignmentOperatorExample.java**

Second Value is:- 15

After Multiplication Second Value is:- 75

Here in this simple example we learn how simple assignments operator is used to assign value to variable.

### 6. Other Operators:

Above Listed Operators are main purposed operators while some of Operator that java uses should be put into another category. Some of operators that java uses which do not fit in the above operators category are listed below.

- new.
- Ternary operator (?:).

### 1. new:

In java some people have confusion about new keyword. As new is a keyword in java language but it also one kind of Operator. New operator in java is used for allocating memory to the object. We can create instance of class in java using new operator.

**For an
example:**

MyClass
object=**new**MyClass();

Here in above example new operator assigns memory to object. We create instance of MyClass using new keyword.

### 2. Ternary Operator:

Ternary operators in java works
like if else control. In comparison to if else, ternary operator is easy to use**. **By using ternary operators you can
check condition. The portion after “?” is
true part portion that will execute when given condition is true and portion
after “:“ will execute if condition is false.

**Syntax:**

(Condition)?True:false;

Example of ternary operator is given below:

**Listing 8: TerneryOperatorExample.java**

public class TerneryOperatorExample { public static void main (String[] args) { intfirstNumber=10; intsecondNumber =5; String maxNumber=(firstNumber>secondNumber)?"firstNumber "+firstNumber+ " is Max Number":"secondNumber "+ secondNumber+" is Max Number"; System.out.println("\n\n "+ maxNumber); secondNumber=15; maxNumber=(firstNumber>secondNumber)?"firstNumber "+firstNumber+ " is Max Number":"secondNumber "+ secondNumber+" is Max Number"; System.out.println("\n\n "+ maxNumber); } }

**Output Listing 8: TerneryOperatorExample.java**

firstNumber 10 is Max Number

secondNumber 15 is Max Number

In above example we use ternary operator that check maximum number.

### Conclusion

Here in this article we learnt about operators and got some idea about how different operators work in Java. There are many types of operators in java and now we have idea about where to use which type of operator.