## Java Basic Operators

Operator in java is a symbol that is used to perform some operations. Java provides a rich set of operators for manipulating variables. They are,

• Arithmetic operators
• Relation operators
• Logical operators
• Bitwise operators
• Assignment operators
• Conditional operators
• instanceof operators

### 1. The 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:

 Operator Description Example (A=15,B=10) + Addition - Adds the two operands on either side of the operator A + B = 25 - Subtraction - Subtracts second operand from first operand A-B=5 * Multiplication - Multiplies the two operands on either side of the operator A*B=150 / Division - Divides numerator by denominator A/B=1 % Modulus- Divides numerator by denominator and returns remainder value A%B=5 ++ Increment - Increases integer value of operand by one A++=16 -- Decrement - decreases integer value of operand by one A--=14

### 2. The Relational Operators:

The following table shows all relational operators supported by Java language.

 Operator Description Example (A=15,B=10) == Checks if the values of two operands are equal or not, if values are equal then condition becomes true. (A == B) Condition is false != Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (A!=B) Condition is true > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A>B) Condition is true < Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A>=B) Condition is true <= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A<=B) Condition is false

### 3. The Logical Operators:

The following table shows all logical operators supported by Java language. Assume A=1 B=0

 Operator Description Example (A=1,B=0) && Logical AND operator - If both the operands are true (non-zero), then the condition becomes true. (A && B) Condition is false || Logical OR Operator- If any of the two operands are true (non-zero), then the condition becomes true. (A||B) Condition is true ! Logical NOT Operator- Use to reverses the logical operation of its operand. If a condition is true then Logical NOT operator returns false. !(B) Condition is true

### 4. The Bitwise Operators:

The bitwise operators can be applied to the integer types long, int, short, char and byte. It operates on individual bits of integer (int and long) values (bit-by-bit operation).

Operator Description Example (A=3,B=5) Output
Decimal Binary
& Binary AND- sets a bit to 1 if and only if both of the corresponding bits in its operands are 1, and to 0 if the bits differ or both are 0 (A & B) 1 001
| Bitwise OR - sets a bit to 1 if one or both of the corresponding bits in its operands are 1, and to 0 if both of the corresponding bits are 0.. (A|B) 7 0111
^ Bitwise XOR - sets the bit to 1 where the corresponding bits in its operands are different, and to 0 if they are the same. (A^B) 6 0110
~ Bitwise NOT- This Ones Complement Operator takes only one parameter and inverts each bit in the operand, changing all the ones to zeros and zeros to ones. This is useful when "unsetting" or "turning off" a flag. (~A) -4 11100(in 2's complement form due to a signed binary number)
<< Bitwise Left Shift Operator-The left operands value is moved left by the number of bits specified by the right operand. Zero bits are shifted into the low-order positions. (A<<2) 12 1100
>> Bitwise Right Shift Operator- The left operands value is moved right by the number of bits specified by the right operand. (B>>2) 1 1
>>> Bitwise Shift right zero fill operator- The left operands value is moved right by the number of bits specified by the right operand and Zeros are filled into the high-order positions. (B>>>2) 1 0001

### 5. The Assignment Operators:

The following assignment operators supported by Java language:

 Operator Description Example = assigns values from right side operands to left side operand. A = B+2 assigns the value of (B+2) into A += adds right operand to the left operand and assign the result to left side operand. A+=B equivalent to A=A+B -= subtracts right operand from the left operand and assign the result to left side operand. A-=B equivalent to A=A-B *= multiplies left operand with the right operand and assign the result to left side operand. A*=B equivalent to A=A*B /= divides left operand with the right operand and assign the result to left side operand. A/=B equivalent to A=A/B %= calculate modulus of two operands and assign the result to left operand. A%=B equivalent to A=A%B &= performs Bitwise AND operation and assign the result to left operand. A&=B equivalent to A=A&B |= performs Bitwise OR operation and assign the result to left operand. A|=B equivalent to A=A|B ^= performs Bitwise XOR operation and assign the result to left operand. A^=B equivalent to A=A^B <<= performs Bitwise left shift operation and assign the result to left operand. A<<=2 equivalent to A=A<<2 >>= performs Bitwise right shift operation and assign the result to left operand. A& gt;>=2 equivalent to A=A>>2

### 6. Conditional/ Ternary Operator ( ? : ):

This operator is used to evaluate boolean expressions consists of three operands. The purpose of this ternary operator is to decide which value should be assigned to the variable. It can be used as an alternative to the Java if/then/else syntax. The operator is written as :

##### Syntax:
variablename = (expression1) ? epr2 (if true) : epr2 ( if false)

If Condition of expression1 is true, then it returns value of expr2 : Otherwise returns value of expr3

##### Example: ### 7. instanceof Operator:

It is used for object reference variables. It checks whether the object is of particular type (class type or interface type).

##### Syntax:
( Objectreferencevariable ) instanceof (class/interface type)

It returns true if the left side of the expression is an instance of the class name on the right side.

##### Example: ### Java other Operators:

#### dot (.) operator:-

It is used to select members of a class (data or methods)

##### Example: #### new operator:-

It is used to create objects out of a class

##### Example: ##### Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

##### Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

• 54