# Java Operators – Arithmetic, Unary & Bitwise Operators In Java

In This Tutorial, You Will Learn About Various Java Operators -Assignment, Arithmetic, Unary, Equality and Relational, Conditional, Type Comparison, and Bitwise & Bit Shift Operators:

But before starting with it directly, let’s briefly understand the term “Operators”

Operators are nothing but special symbols. The purpose of these symbols is to perform a specific operation on one, two, or three operands, and then return a result. For Example, symbols like =, < , & , ^ etc . ## Java Operators

Let us now see the operators that are supported in Java language.

Java supports operators of the following categories:

• Assignment Operators
• Arithmetic Operators
• Unary Operators
• Equality and Relational Operators
• Conditional Operators
• Type Comparison Operator
• Bitwise and Bit Shift Operators

### #1) Assignment Operators

We will see one of the commonly encountered operators i.e. Simple assignment operator ‘=’. This operator assigns the value on its right to the operand on its left.

Let’s have look at the following Java sample that illustrates the use of Assignment operators.

```public class AssignmentOperatorDemo{
public static void main(String args[]){
int x=300; //Assigns value on the left of ‘=’ i.e. 300 to it’s left i.e. variable x
int y=700; //Assigns value on the left of ‘=’ i.e. 700 to it’s left i.e. variable y
int z = 0; //Assigns value on the left of ‘=’ i.e. 0 to it’s left i.e. variable z
z = x+y; //Assigns value on the left of ‘=’ i.e. (x+y) i.e. 300+700=1000 to
it’s left i.e. variable z
System.out.println(x); //This prints output as 300
System.out.println(y); //This prints output as 700
System.out.println(z); //This prints output as 1000
}
}
```

This program prints the following output: This operator can also be used on objects to assign object references.

For Example, Car car1 = new Car(); // ‘=’ assigns new Car() object instance to object reference car1.

### #2) Arithmetic Operators

To perform arithmetic operations like addition, subtraction, multiplication, and division, These are identical to that of basic mathematics. The only different symbol is “%”, which is the Modulus or Remainder operator and the purpose of this operand is to divide one operand by another and return the remainder as its result.

Following are the Arithmetic Operators supported in Java :

OperatorDescription
+Additive Operator (also used for String concatenation)
-Subtraction Operator
*Multiplication Operator
/Division Operator
%Modulus or Remainder Operator

Given below is a JAVA sample illustrating the use of Arithmetic operators:

```public class ArithmeticOperatorDemo {
public static void main (String[] args) {
int x = 30;
int y = 20;
int result_value = x + y;
System.out.println("30 + 20 = " + result_value); // This prints o/p 50

result_value = x - y;
System.out.println("30 - 20 = " + result_value);// This prints o/p 10

result_value = x * y;
System.out.println("30 * 20 = " + result_value);// This prints o/p 600

result_value = x / y;
System.out.println("30 / 20 = " + result_value);// This prints o/p 1

result_value = x % y; // Returns remainder of division 30/20 i.e. 10
System.out.println("30 % 20 = " + result_value);// This prints o/p 10
}
}

```

This program prints the following output: ### #3) Unary Operators

The Unary Operators are the operators that need a single operand.

For Example, operations like incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.

Following are the Unary Operators supported in Java:

OperatorDescription
+Unary plus Operator; indicates positive value (numbers are positive without this, however)
-Unary minus Operator; negates an expression
++Increment Operator; increments a value by 1
--Decrement Operator; decrements a value by 1
!Logical complement Operator; inverts the value of a boolean

Given below is a Java sample illustrating the use of Unary Operators:

```public class UnaryOperatorDemo {
public static void main(String[] args) {

int result_value = +10;// indicated positive value 10
System.out.println(result_value); //o/p is 10

result_value --; // decrements the value of 10 by 1
System.out.println(result_value); //o/p is 9

result_value ++; // increaments the value of 9  by 1
System.out.println(result_value); //o/p is 10

result_value = - result_value;// this minus operator negates an expression
System.out.println(result_value); //o/p is -10

booleanisPass = false;
System.out.println(isPass); //o/p is false
System.out.println(!isPass);//o/p is inverted isPass value i.e. true
}
}
```

This program prints the following output: The increment/decrement operators can be used before (prefix) or after (postfix) the operand. Even though, both the values will return the original value being incremented/decremented by one. The difference is, the prefix operator evaluates the operand to the incremented value, whereas the postfix version evaluates the operand to the original value.

Let’s have a look at the following PreAndPostDemo illustrating the prefix and postfix functionality.

```public class PreAndPostDemo {
public static void main(String[] args){
int a = 5;
System.out.println(a++);  // output is 5
System.out.println(a); // output is 6
System.out.println(++a); // output is 7
System.out.println(a++);  // output is 7
System.out.println(a); // output is 8
}
}
```

This program prints the following output: ### #4) Equality and Relational Operators

The equality and relational operators are the operators to compare and determine if one operand is greater than, less than, equal to, or not equal to another operand.

Following are the Equality and Relational Operators supported in Java :

OperatorDescription
==Equal to
!=Not equal to
Greater than
>=Greater than or equal to
Less than
<=Less than or equal to

See the following Java sample illustrating the use of Relational Operators:

```public class RelationalOperatorDemo {
public static void main(String[] args){
int a = 5;
int b = 10;
boolean resultFlag = (a == b);
System.out.println("a == b :"+ resultFlag);//o/p is false as 5 is not equal   to 10
resultFlag = (a != b);
System.out.println("a != b :"+ resultFlag); //o/p is true as 5 is not equal   to 10
resultFlag = (a > b);
System.out.println("a >b :"+ resultFlag); //o/p is false as 5 is not greater   than 10
resultFlag = (a < b);
System.out.println("a <b :"+ resultFlag); //o/p is true as 5 is less than 10
resultFlag = (a <= b);
System.out.println("a <= b :"+ resultFlag); //o/p is true as 5 is less than 10
resultFlag = (a >= b);
System.out.println("a >= b:"+ resultFlag); //o/p is false as 5 neither greater than 10      nor equal to 10
}
}
```

This program prints the following output : ### #5) Conditional Operators

Java supports conditional operators, || and && for performing Conditional-OR and Conditional-AND operations on two boolean operands. This behavior is also called as “short-circuiting” behavior. In this behavior, the second operand evaluation takes place only if required.

Another conditional operator supported is the ternary operator  ‘?:’ which is called as shorthand for an if-then-else statement.

OperatorDescription
&&Conditional-AND
||Conditional-OR
?:Ternary (shorthand for
if-then-else statement)

Given below is a Java sample illustrating the use of Conditional  Operators:

```public class ConditionalOperatorDemo {
public static void main(String[] args){
int a = 5;
int b = 10;
boolean resultFlag = ((a == 5) && (b == 10));
//o/p is true as both conditions are evaluated true
System.out.println("a is 5 AND b is 10 :"+resultFlag);
resultFlag = ((a == 5) || (b == 5));
//o/p is true as one of the  conditions is evaluated true
System.out.println("a is 5 OR b is 5:"+ resultFlag);
//Because resultFlag is true,  finalResult will be assigned “Pass”.
String finalResult = resultFlag?"Pass":"Fail";
System.out.println("finalResult:"+ finalResult);//o/p is “Pass”
}
}
```

This program prints the following output: ### #6) Type Comparison Operator

OperatorDescription
instanceofCompares an object to
a specified type

The purpose of the instance of an operator is to compare an object to a specified type. This can be used to test if an object is an instance of a class, a subclass, or a class that implements a particular interface.

Let’s have look at the following Java sample illustrating the use of Comparison Operators:

```publicclass Shape {}
public class Square extends Shape implements Area {}
public interface Area {}

publicclassInstanceofOperatorDemo {
publicstaticvoidmain(String[] args) {
Shape shape1 = newShape();
Shape shape2 = newSquare();

System.out.println("shape1 instanceof Shape: " + (shape1 instanceof Shape));
System.out.println("shape1 instanceof Square: " + (shape1 instanceof Square));
System.out.println("shape1 instanceof Area:" + (shape1 instanceof Area));
System.out.println("shape2 instanceof Shape: " + (shape2 instanceof Shape));
System.out.println("shape2 instanceof Square: " + (shape2 instanceof Square));
System.out.println("shape2 instanceof Area: " + (shape2 instanceof Area));
}
}
```

This program prints the following output: ### #7) Bitwise And Bit Shift Operators

Java also supports operators for performing Bitwise and Bit shift operations on any of the integer types i.e. long, int, short, char, and byte.

Following are the supported Bitwise and Bit shift Operators:

OperatorDescription
~Unary bitwise complement
<< Signed left shift
>> Signed right shift
>>> Unsigned right shift
&Bitwise AND
^Bitwise exclusive OR
|Bitwise inclusive OR

Let’s have a look at the following  Java sample that illustrates the use of Bitwise Operators:

```public class BitwiseOperatorDemo {
public static void main(String[] args)
{
//Initial values
int x = 5;
int y = 6;
int z = 2;

//binary value of x=5 is 0101
// binary value of y=6 is 0110
// binary value of z = 2 0010

// bitwise and  0101& 0110=0100 = 4
System.out.println("x&y = " + (x & y));

// bitwise or 0101 | 0110 = 0111 = 7
System.out.println("x|y = " + (x | y));

// bitwise xor  0101 ^ 0110 = 0011 = 3
System.out.println("x^y = " + (x ^ y));

// bitwise complement of 5 i.e.~0101=1010 returns  2's complement of 1010 = -6
System.out.println("~x = " + ~x);

// left shift operator 0010 returns 1000 i.e.8
System.out.println("z<<2 = " + (z << 2));

// right shift operator 0110 returns 0001 i.e.1
System.out.println("x>>2 = " + (x>> 2));

// unsigned right shift operator for y=6 i.e.0110 returns 0001 i.e.1
System.out.println("y>>>2 = " + (y >>> 2));

}
}```

This program prints the following output: ### Java Operator Precedence

So far, we have explored the operators supported in Java. Now let’s have look at the precedence of these operators. The operators are listed as per their precedence in descending order in the following table. Postfix is having the highest precedence and assignment is the lowest precedence operator.

Significance of Precedence: Evaluation of Operators takes place as per the operator precedence i.e. Evaluation takes place starting with the operators of higher precedence and is followed by operators having relatively lower precedence.

All Binary Operators are evaluated from left to right and the only exception is Assignment Operators. In the case of Assignment operators, the operator’s evaluation takes place from right to left.

Operator Precedence
OperatorsOperators PrecedenceAssociativity
Postfixexpr++ expr--Left to right
Unary++expr --expr +expr -expr ~ !Right to left
Multiplicative* / %Left to right
Shift<<>>>>> Left to right
Relational <><= >= instanceofLeft to right
Equality == !=Left to right
Bitwise AND&Left to right
Bitwise exclusive OR^Left to right
Bitwise inclusive OR|Left to right
logical AND&&Left to right
logical OR||Left to right
Ternary? :Right to left
Assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=Right to left

Q #1) What are the Operators that are used in Java?

Answers: Operators in Java are special symbols. The purpose of these symbols is to perform specific operations on one, two, or three operands and return a result.

For Example, symbols like =, < , & , ^ etc .

Q #2) What is === Operator in Java?

Answers: === operator is called a strict equality operator in Javascript. This operator returns true if both the variables are of the same type and also contains the same value.

For Example, 1===”1″//This will return false. This is because both the operands are not of the same type.

== operator in Javascript compares two variables of different types by automatically converting one type to another.

For Example, 1==”1″ This will return true. Here, the string gets converted into number and comparison takes place.

Q #3) What is Java Assignment Operator?

Answers: Java assignment operator i.e the ‘=’ operator assigns the value on its right to the operand on its left.

For Example, int x = 300; Here ‘=’ assigns value 300 to variable x

Q #4) What is == in Java?

Answers: == operator in Java is used to compare reference i.e. this operator verifies if both the objects point to the same memory location

This differs in .equals() that does the comparison of values in the objects.

For Example,

String str1 = new String(“Good Morning”);
String str2 = new String(“Good Morning”);
System.out.println(str1 == str2);//This returns false as this compares addresses i.e. memory locations of two objects
System.out.println(str1.equals(str2));//This returns true as it compares value.

Q #5) How many Types of Operators are there in Java?

Answers: Enlisted below are the various types of Operators in Java:

• Assignment Operator
• Arithmetic Operators
• Unary Operators
• Equality and Relational Operators
• Conditional Operators
• Type Comparison Operator
• Bitwise and Bit Shift Operators

Q #6) What is the use of the Dot Operator in Java?

Answers:  The dot operator or separator or period in Java is used to separate a variable i.e. the method from an object reference variable.

For Example, Car car1 = new Car();

car1.name = “Audi”; // Here ‘.’ is used to access the field ‘name’ of Car object reference ‘car1’

Q #7) What are the 3 Logical Operators?

Answers: Logical operators operate on the Boolean operand.

Following are the logical operators:

• && : Logical AND
• || : Logical OR
• ! : Logical Not

Q #8) What are Bitwise Operators in Java?

Answers:  Java supports operators for performing bitwise and bit shift operations on any of the integer types i.e. long, int, short, char, and byte.

Following are the supported Bitwise and Bit shift operators :

OperatorDescription
~Unary bitwise complement
<< Signed left shift
>> Signed right shift
>>> Unsigned right shift
&Bitwise AND
^Bitwise exclusive OR
|Bitwise inclusive OR

## Conclusion

In this tutorial, we have explored the different types of operators supported in Java along with their purpose.

In a nutshell, the Java Operators include:

• Assignment Operator
• Arithmetic Operators
• Unary Operators
• Equality and Relational Operators
• Conditional Operators
• Type Comparison Operator
• Bitwise and Bit Shift Operators

We also saw how these operators are used in the Java code with the help of some examples illustrating the usage of these operators. Even though we have seen all the types of operators, the usage of these operators in general-purpose programming may vary at times.

Some of the operators usually appear more frequently than the others like the assignment operator “=” is far very commonly used in code than the unsigned right shift operator “>>>

We will see each of these operator categories in detail in our upcoming tutorials.