**Operators In Java**

**An operator is a symbol that tells the compiler to perform specific mathematical or logical Operation.**

Java language is rich in built-in operators and provides the following types of operators −

Let us discuss these operators in details.

**Arithmetic Operators**

To do arithmetic, Java uses operators. It provides operators for five basic arithmetic calculations : addition, subtraction, multiplication, division and remainder which are +, – , * , / and % respectively.

Each of these operators is a binary operatos i.e., it requires two values (operands) to calculate a final answer.

Apart from these binary operators, Java provides two unary arithmetic operators (that require one operand) which are unary + and unary – .

**Unary Operators**

**Operators that act on one operand are referred to as Unary Operators**

**→ Unary +** . The Operator unary ‘+’ precedes an operand(the value on which the operator operates).

**For example: if a=5 then +a means 5.**

**→ Unary –** . The operator unary ‘-‘ precedes an operand. This operator reverses the sign of the operand’s value.

**For example: if a=5 then -a means 5.**

**Binary Operators**

**Operators that act upon two operand are referred to as Binary Operators**

The operands of a binary operator are distinguished as the left or right operands. Together, the operator and its operands constitute an expression.

**[1] Addition operator +.** The arithmetic binary operator + adds values of its operands and the result is the sum of the values of its two operands

**For example,**

4+20 results in 24.

a+5 results in 7,when a = 2

a+b results in 10,when a = 4, b = 6

Its operands may be of Integer (byte, short, char, long) or of Real ( float, double) types.

**[2] Subtraction operator -.** The operator + subtracts the second operand from the first.

**For example,**

14 – 3 evaluates to 11.

a – b evaluates to 2,when a = 7, b = 5

The operands may be of Integer or float Real types.

**[3] Multiplication operator *.** The operator * multiplies the values of its operands

**For example,**

3 * 4 evaluates to 12.

b * 4 evaluates to 24,when b = 6

a * c evaluates in 15,when a = 3, c = 5

The operands may be of integer or float types.

**[4] Division operator /.** The operator / divides its first operand by the second.

The division operator in Java , just like C language uses two kinds of division :

Integer Division : if both operands(Numerator and Denominator) are integers then result will also be an integer. So

10/4 is 2 not 2.5Floating Division: if either operands (Numerator or Denominator or both) are float or double then result will also be float or double. So

10/4.0 is 2.5

The operands may be of integer or float types.

**[5] Modulus operator %.** The operator % finds the modulus of its first operand relative to the second. That is, it produces the remainder of dividing the first by the second operand

Few Important Points:The modulo division operator( % ) in Java , unlike C , works with float and double data type also.

So

10%5evaluates to0, since 5 goes into 10 two times with a remainder 0.So

10.5 %5.0will evaluates to0.5as the remainder

**Operator + with Strings**

You have used the operator ‘+’ with numbers. When you use + with numbers, the result is also a number. However, if you use operator + with strings it concatenates them and gives a string .

Lets see some examples :

**5 + 6** results into 11.

**“5” + “6”** results into “56”.

**“17” + ” B, Raj Nagar”** results into “17 B, Raj Nagar”

**“abc” + “123”** results into “abc123”

**5 + “xyz”** results into “5xyz”

**Relational Operators**

The term relational in relational operator refers to the relationships that values ( operands ) can have with one another. Thus, the relational operator determines the relations among different operands.

Java provides six relational operators for comparing numbers and characters. If the comparison is true, the relational expression results into the boolean value **true** and to the boolean value **false**, if comparison is false.

The six relational operators are:

**< **( less than ),

**<= **( less than or equal to ),

**== **( equal to ),

** > ** ( greater than ),

** >= ** ( greater than or equal to ) and

** !=** ( not equal to )

Note: But they don’t work with Strings

**→ Summarizes the action of theses relational operator**

p |
q |
p < q |
p <= q |
p == q |
p > q |
p >= q |
p != q |

0 | 1 | t | t | f | f | f | t |

1 | 0 | f | f | f | t | t | t |

3 | 3 | f | t | t | f | t | f |

2 | 6 | t | t | f | f | f | t |

**Logical Operators**

Relational operators often are used with logical operators ( also known as conditional operators sometimes ) to construct more complex decision-making expressions.

The Java programming language supports six conditional operators– five binary and one unary

**The logical** **AND** **operator** **(&&)**

The logical AND operator, written as &&, combines two expressions into one. The resulting expression has the result as Boolean value **true** only if both of the original expressions (its operands) are true

Following are some examples of AND operator (&&).

(6==3)&&(4==4) , results into false because first expression is false

(4==4)&&(8==8) , results into true because both expressions are true.

6 < 9 && 4 > 2 , results into true because both expressions are true.

6 > 9 && 5 < 2 , results into false because both expressions are false.

Since Logical AND operator (&&) has lower precedence than relational operators, we don’t need to use parentheses in these expressions.

**The logical** **OR** **operator** **( || )**

The logical OR operator, written as ||, also combines two expressions into one. The logical OR (“||”)operator gives the result as Boolean value **true** if either of its operands evaluate to true

This principle is used while testing evaluating expressions. Following are some examples of logical OR operation :

(5==3)||(4==4) , results into true because second expression is true.

0||0 , results into false because both expressions are false.

5 > 8||5 < 2, results into false because both expressions are false.

0 || 8 , results into true because second expressions is true.

Since Logical OR operator (||) has lower precedence than relational operators, we don’t need to use parentheses in these expressions.

**The logical** **NOT** **operator** **( ! )**

The logical NOT operator, written as !, works on single expression or operand i.e., it is a unary operator.

The logical NOT operator (!) negates or reverse the truth value of the expression following it i.e, if the expression is **true**, then **!expression** is **false**, and vice versa

Following are some examples of logical NOT operators’ usage :

!(5!=3), results into false because 5!=0 returns true.

!(5 > 2), results into false because the expression 5 > 2 is true.

!(5 > 9> , results into true because the expressions 5 > 9 is false

The logical negation operator ! has a higher precedence than any of the relational or arithmetic operators. Therefore, to negate an expression, you should enclose the expression in parentheses :

**!(x > 5)** will reverse the result of the expression x > 5

whereas !x > 5 is equivalent to (!x) > 5 i.e., it will first reverse the truth value of x and then test whether the reverse of x’s truth value greater than 5 or not.

**Assignment Operators.**

Like other programming languages, Java offers an assignment operator =, to assign one value to another eg,.

int x,y,z;

x=9;

z=7;

z = x + y;

z = z * 2;

**Java Shorthand Operators**

Java offers special shorthand that simplify the coding of a certain type of assignment statement.

For example, a = a + 10;

can be written as a += 10;

The operator pair += tells the compiler to assign to a the value of a + 10. This shorthand works for all the binary operators in Java ( those that require two operands ).

The general form of Java shorthand is :

var=varoperatorexpression

is same as

varoperator =expression

Following are some examples of Java shorthands:

x -= 10; equivalent to x = x – 10;

x *= 3; equivalent to x = x * 3;

x /= 2; equivalent to x = x / 2;

x %= z; equivalent to x = x % z;

Java shorthand just shortens the written expression, it does not fasten the execution,

e.g. , x += 10 is not faster than x = x + 10.

**Summarizes the action of theses relational operator**

A is **true** and B is **false**