# Operators in Java

Operators in Java

Any symbol that is used for performing and operation can we called as an “operation”. The values on which the operator performs the operations are called as “operands”.

Eg:   x + y      Here x, y are operands and ‘+ ‘is the operator.

The operators are classified into different types based on the number of operands it considers

1. Unary operators:

The operator that performs the operation on a single operand is called as “unary operator”.

2. Binary operator:

An operator that performs the operation on two operands are called as “Binary operators”.

3. Ternary operators:

An operator that performs the operation on three operands are called as “ternary operators”.

The operators are classified into various categories based on the task their performed.

I. Arithmetic operators:

Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators:
Assume integer variable A holds 7 and variable B holds 2, then:

Operator    Description    Example

+    Addition – Adds values on either side of the operator    A + B will give 9

–    Subtraction – Subtracts right hand operand from left hand operand    A – B will give 5

*    Multiplication – Multiplies values on either side of the operator    A * B will give 14

/    Division – Divides left hand operand by right hand operand    A / B will give 3

%    Modulus – Divides left hand operand by right hand operand and       returns remainder    A % B will give 1

++    Increment – Increases the value of operand by 1    B++ gives 3

—    Decrement – Decreases the value of operand by 1    B– gives 1

•    If both the operands are of integer type then the result will be of integer type.

•    If at least one of the operand is of floating point type then the result will be of floating point type.

II.unary operators:

These operations are used to perform the operations on a single operand .The  various unary operators are -,–,++.

i.- (unary minus):This operator can we used for converting a positive value to negative value and negative value to positive .

Eg: int termp = 23;
int temp=-23;
int temp=-(-23);
ii.++(increment):

This operator will increase the value of the variable by 1.we have two types of increment operators and the classification is based on the location where the increment operator is placed.

•    Pre increment operator: If the  increment operator is placed before the variable then it is Called as preincrement operator (++X).The pre increment operator will increase the value of the variable before using it.
•    Post increment operator :  If the  increment operator is placed  after  the variable then it is Called as pre increment operator (X++).The post increment operator will increase the value after using it(x++).

iii. Decrement operator:
This operator will decrease the value of the variable by 1.we have two types of decrement operators and the classification is based on the location where the decrement operator is placed.

•    Pre decrement operator:   If the decrement operator is placed before the variable then it is Called as pre decrement operator (–X).The pre decrement  operator  will  decrease the value of the  variable by 1 before using it.
•    Post  decrement operator(x–):   If the  decrement operator is placed after  the variable then it is Called as post decrement operator (X–).The post  decrement  operator  will  decrease the value of the  variable by 1 after using it.

Eg: class operators {

Public static void main(String[]args){
int a=6;
int b=++b;
int c=b–;
int d=a+b++—c;
System.out.println(a);  o/p – >7
s.o.p (b);  o/p ->7
s.o.p (c);  o/p ->6
s.o.p (d)  o/p ->7;
}
}

III.Assignment operator(=):

This operator is used for assigning a value to a variable.The  assignment  operator  =  (is equal to)

The assignment operator will copy to right side value to left side value on the left side should always contain a variable on the right side we can write a value or a variable or a expression.

Operator    Description    Example
=    Simple assignment operator, Assigns values from right side operands to left side operand    C = A + B will assign value of A + B into C
+=    Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand    C += A is equivalent to C = C + A
-=    Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand    C -= A is equivalent to C = C – A
*=    Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand    C *= A is equivalent to C = C * A
/=    Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand    C /= A is equivalent to C = C / A
%=    Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand    C %= A is equivalent to C = C % A
<<=    Left shift AND assignment operator    C <<= 2 is same as C = C << 2
>>=    Right shift AND assignment operator    C >>= 2 is same as C = C >> 2
&=    Bitwise AND assignment operator    C &= 2 is same as C = C & 2
^=    bitwise exclusive OR and assignment operator    C ^= 2 is same as C = C ^ 2
|=    bitwise inclusive OR and assignment operator    C |= 2 is same as
C=C|2

Conditional Operator ( ? : ):

Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide which value should be assigned to the variable. The operator is written as:
variable x = (expression) ? value if true : value if false
Following is the example:
public class Test {

public static void main(String args[]){
int a , b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( “Value of b is : ” +  b );

b = (a == 10) ? 20: 30;
System.out.println( “Value of b is : ” + b );
}
}
This would produce the following result:
Value of b is : 30
Value of b is : 20

Precedence of Java Operators:

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator:

For example, x = 7 + 3 * 2; here x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.
Category    Operator    Associativity
Postfix    () [] . (dot operator)    Left to right
Unary    ++ – – ! ~    Right to left
Multiplicative    * / %    Left to right
Additive    + –    Left to right
Shift    >> >>> <<    Left to right
Relational    > >= < <=    Left to right
Equality    == !=    Left to right
Bitwise AND    &    Left to right
Bitwise XOR    ^    Left to right
Bitwise OR    |    Left to right
Logical AND    &&    Left to right
Logical OR    ||    Left to right
Conditional    ?:    Right to left
Assignment    = += -= *= /= %= >>= <<= &= ^= |=    Right to left
Comma    ,    Left to right

instanceOf Operator:

This operator is used only for object reference variables. The operator checks whether the object is of a particular type(class type or interface type). instanceOf operator is wriiten as:
( Object reference variable ) instanceOf  (class/interface type)
If the object referred by the variable on the left side of the operator passes the IS-A check for the class/interface type on the right side, then the result will be true. Following is the example:
String name = = ‘James’;
boolean result = name instanceOf String;
// This will return true since name is type of String
This operator will still return true if the object being compared is the assignment compatible with the type on the right. Following is one more example:
class Vehicle {}

public class Car extends Vehicle {
public static void main(String args[]){
Vehicle a = new Car();
boolean result =  a instanceof Car;
System.out.println( result);
}
}

o/p: true

0
in Java