Skip to main content

Operators

You have just learnt how to declare variables. Now let's know  what we can do with the variables.


Operators takes some input and returns the result back.


For example,

The expression a + b returns the sum of a and b;

We can store the result in another variable also.

c = a + b;

Then the sum of variable a and variable b is stored in variable c.


The operators supported by Java are : 


Arithmetic operators : (works on two variables)

+ : returns the sum of two variable

- : returns the difference between two variables

* : returns the multiplication of two variables

/ : returns the quotient by dividing one variable with the other

% : returns the remainder by dividing one variable with the other



Comparison operators : (works on two variables)

== : returns true if two variables has the same values. otherwise returns false.

!= : returns false if two variables has the same values. otherwise returns true.

> : returns true if first variable is greater than second one, otherwise returns false.

>= : returns true if first variable is greater than or equals to second one, otherwise returns false.

< : returns true if first variable is less than second one, otherwise returns false.

<= : returns true if first variable is less than or equals to second one, otherwise returns false.




Logical operators : (works on two variables of type boolean only)

&& : If any one of the variable is false, it returns false. otherwise returns true. 

| | : If any one variable is true, returns true, otherwise returns false.

! : (works on single variable of type boolean) return the negation of the variable.




Bitwise operators : (works on two variables)

& : returns the bitwise AND of two variables.

| : returns the bitwise OR of two variables.

^ : returns the bitwise XOR of two variables.

~ : (works on single variable) returns the bitwise NEGATION of the variable.

<< : (works on single variable) returns the bitwise LEFT SHIFT of the variable.

>> : (works on single variable) returns the bitwise RIGHT SHIFT of the variable.


Left shift and right shift operator are more powerful. So, try to use them in the place of multiplication or division operators.

Left shift operator multiplies the given number by 2.

Similarly, right shift operator divides the given number by 2 and returns the quotient.

Examples :

5 * 21  is same as 5 << 1

7 * 2is same as 7 << 2

4 * 2is same as 4 << 3

...


5 / 21  is same as 5 >> 1

12 / 2is same as 12 >> 2

15 / 2is same as 15 >> 3

...


5 * 13 is same as 5 * (8 + 4 + 1)  =>  (5 * 8+ 5 * 4 + 5 * 1) 

which can be written as (5 << 3 + 5 << 2 + 5)


In the above problem, we can simplify any one number as the sum of powers of 2.

like [1, 2, 4, 8, 16, 32, 64, 128, ......)

13 = (8 + 4 + 1)

5 = (4 + 1)


13 * 5 is same as 13 * (4 + 1)  =>  (13 * 4 + 13 * 1) 

which can be written as (13 << 2 + 13)


Assignment Operators : 

= : assigns the right side value to the left side variable.

We can also combine arithmetic operators with the assignment operators as follows :

For example,

a += b, makes the sum of a and b and assign the result to variable a.




Pre Increment and Decrement Operators :

(++ , --)

These operators are placed just before the variable to increment or decrement the value of the variable by 1.

Example : ++a , --b

++a is same as (a = a + 1)

--b is same as (b = b - 1) 


Post Increment and Decrement Operators :

(++ , --)

These operators are placed just after the variable to increment or decrement the value of the variable by 1.

Example : a++ , c++

a++ is same as (a = a + 1)

c-- is same as (c = c - 1)


Confused !

The difference between ++a and a++ is : 

++a changes the value immediately at that moment where as a++ modifies the value of a after the execution of current statement. 

 


NEXT

Comments