Java

Pg-7

Operators are nothing but a symbol that specifies an operation (arithmetic,Boolean etc.) to be performed over the given operands.

**Associativity :**The direction in which the operations are carried (left to right,right to left).

**Order of precedence :**The order in which the operations are performed. Following are some of the operators in java

- Assignment operators
- Unary operators
- Arithmetic operators
- Equality operators
- Relational operators
- Conditional and Boolean operators
- Bitwise operators
- Bit shift operators
- Conditional operators
- Special operators

It is an operator which assigns the value of the operand on the right side of the operator(=) to the operand on the left side.

**For (eg):-**

int a = b+c;

Computes the sum of the operands b and c and assigns the resultant value to a(operand on the left side of "=")

It is an operator which operates with only one operand.

**Associativity :** right to left.

Following are the available unary operators

This operator increments the value of the associated operand by one.

This operator decrements the value of the associated operand by one.

This operator inverts the Boolean value from "false to true" or "true to false".

This operator indicates that the associated operand is positive.

This operator negates the value of an operand.

class OperatorsDemo { public static void main(String[] args) { int a=10,b,c,d; boolean flag=false; b=a++; //assigns 10 to b c=a--; //assigns 11 to c d=-a; //assigns -10 to d System.out.println("Value of a :"+a+"\nValue of b :"+b+"\nValue of c:"+c+"\nValue of d :"+d); System.out.println("Currently flag="+flag); System.out.println("flag after negation/logical complement="+(!flag)); } }

Value of a :10 Value of b :10 Value of c :11 Value of d :-10 Currently flag=false flag after negation/logical complement=true

**+**is used to add the operands**-**is used to subtract the value of the operands*****multiplies the value of the given operands**/**finds the quotient of the division of two operands**%**finds the reminder of the division of two operands

**Note :**

in java,+ operator can also be used to concatenate two String values.

"==" and "!=" are the two equality operators that checks whether the two operands are equal or not.
**Associativity** : left to right.

Generally these operators are used in the conditional or looping structure such as if,while,do while etc.

Following are the relational operators which are used to compare two operands

**>**Greater than**<**Lesser than**>=**Greater than equal to**<=**Lesser than equal to

**Associativity** : left to right.

**&&**conditional AND - returns true only if both the expressions/operands are true and it will not check the second expression if the first expression is false**||**conditional OR - returns true if either of the two expression/operands is true and it will not check the second expression if the first expression is true**&**Boolean AND - returns true only if both the expressions are true and both the expressions are evaluated**|**Boolean OR - returns true if either of the two expressions is true and both the expressions are evaluated**^**Boolean XOR - returns true if one of the expression is true and the other one is false.

int a=10,b=11; if((a==10) && (b==11)) System.out.println("both the expressions are true"); if((a==10) || (b==10)) System.out.println("one of the expression/both the expression are true"); if((a==10) ^ (b==10)) System.out.println("one of the expression is true and the other one is false");

**Syntax : expression?statement1:statement2;**

Evaluates the expression. If the result is true,then it will execute statement1,otherwise it will execute statement2 This is similar to an if..else statement.

int a=10,b=9; int c=(a>10)?a:b;

if a is greater than 10,a will be assigned to c,else b will be assigned to c.

Following are some of the special operators available in java

**instanceof**- checks whether the instance belongs to a particular class**new**- creates a new instance of a class**dot operator .**- used to access a member(field or method) of a class

Post your comment## Says:User