what is operator in java

Operators in java with examples :

Operator: operator is used to perform operation on the operands . a question arises what is operand ? it is simple as operator . operand can be defined ass on which is operation is going to be done .  for example if addition operation is performing on a and b . then we can say as a and b are operands and addition (+) is operator.

In this example :a + b  , operation =addition , operand =a , b

There are many types of operators in java :

  1. Arithmetic operator 
  2. Unary operator
  3. Relational operator
  4. Logical operator
  5. Assignment operator
  6. Ternary operator
  7. Bitwise operator

 1. Arithmetic Operator

Arithmetic operator is used to perform the mathematical  operation by the help of arithmetic operator on the operands( variable ).  Syntax : operand1 operator operand 2 ;

  • Addition : + 
  • Subtraction : -
  • Multiplication : *
  • Division: /
  • Modulo: %

Addition : It gives the Summation of operand or variables.

example of  addition  operator.
int a=5, b=2,c;
c =a+ b;  // Output  7  
  

 Subtraction : This operation is performed to find the  difference between the operand.

example of subtraction operator :
int sub , a=5 , b=2 ;
sub=a-b;   // Output  3

Multiplication : This operator is used to find the product of operand .

example of multiplication operator :
int mul , a=2,b=3;
mul=a*b ; //Output 6 


 Division : It is used to find the quotient .

example of Division operator:
int div , a=2,b=6;
div=b/a;  //Output 3

Modulo : This operator is used to find the remainder .

example of modulo operator
int mod , a=2,b=6;
mod=b% a; // Output 0

2.Unary  Operator :

 Unary operator is used with a operand . or only one operand or variable is used.  

unary operator is also divided in two types : 

  • Increment operator
  • Decrement operator    

Increment operator : Increment operator is used to increment the value by 1.or  in increment operator the value of operand is increased by 1. 

Types of increment operator :
a .pre-increment 
b. post increment

a. pre-increment : In this the value of  operand is firstly increased by 1 then we use the value.

Syntax: + + operand ;

example : + + a ;  If the value of a is 5 , first value of a is increased by 1 means 6 . Then we use the value of a in operation .

b. post-increment : In this the value of operand is used first then the value is increased by 1.
 Syntax : operand + +;

example : a + +; if the value of a is 5 , first it uses the value of a then the value is increased by 1.

Decrement operator : decrement operator is  used to decrement the value by 1.

Type of decrement operator:

a. pre-decrement
b. post-decrement

a. pre-decrement : It first decrease the value then use the value. 

Syntax : - - operand; example : - -a ; if the value of a is 5 ,first it decrease the value by 1 means 4 , then use the value .

b. post-decrement : It first uses the value of operand then decrease the value by  one.
Syntax: operand - -;
example : a - -; first it uses the value  then the value is decreased by 1 . 

3. Relational Operators : 

These operators are used to established the relation between the operand.
example:
>   Greater than  example : (5>6)
<   Less than  example :(3<5)
>=Greater than equal to  , examples : (3>=5) , (4>=4)
<= Less than equal to ,examples :(22<=30) ,(78<=78)
== equal to (3= =3)
!=  Not equal to (4!=3)

4.Logical operator :

 Logical operators are used to perform logical AND , logical OR and logical NOT operations . It is used to check whether the expression is true or false.

logical AND operator :

 If there is given two condition if both conditions are true then only overall condition is true . Among the two if any one , or both the condition is false, then overall condition will be false . It is denoted by && . Syntax : condition1 && condition2 , if both condition are true then only overall condition is true.

example1:
int a=20 ,b=10,c=3;
if (a>b && a>c)
{
System.out.println("a is greater");
}
else
{
System.out.println("condition false");
}

OUTPUT : a is greater , because 20 is greater than 10 as well as 3.

example 2:

int a=20 ,b=10,c=33;
if (a>b && a>c)
{
System.out.println("a is greater");
}
else
{
System.out.println("condition false");
}

OUTPUT : condition false ,because 20 is greater than 10 but smaller than 33.

Logical OR :

 Among the two condition if anyone condition or both conditions are true then overall condition is true. If both condition are false then overall condition is false. It is denoted by | |.

example1:
int a =38 ,b=18 ,c=3;
if(a>b| |a<c )
{
System.out.println("condition is true");
}
else
{
System.out.println("condition true");
}

OUTPUT: condition is true , because both condition are true .

example2:
int a =38 ,b=18 ,c=3;
if(a>b| |a>c )
{
System.out.println("condition is true");
}
else
{
System.out.println("condition true");
}

OUTPUT: condition is true ,because if any one condition is true means overall condition is true.

Logical NOT ( ! ):

 It is  not like AND  and OR operator .In this if the condition is true it gives false . if condition is false it gives true . It is denoted as ! . Syntax : ! (condition ) , Only one condition is required.  

example :
int a=5,b=4;
System.out.print (!(a>b)); //!(true )  : output will be false .
System.out.println(!(a<b)); //! (false) : output will be true.

5.Assignment operator : 

These operators are used to assign the value . for example a=5, in this = is  a assignment operator. value 5 is assign to a . there are some  more  Assignment operators .

  • + =  Example : a + = b which means add b in a , a= a + b  
  • - =  Example: a - = b which means subtract b from a , a=a-b
  • * = Example : a*=b which means multiply b with a , a=a*b 
  • /= Example : a/=b which means a=a/b

6. Ternary Operator :

It contain three field . If the condition is true then second field means true part  will execute otherwise third field means false part.

 for example : (a>b ? a : b) here a>b is condition  , a is true part and b is false part.  if the value of a =3 , b=7.

(3>7 ? 3 :7)
3>7 //condition false. so false part will execute means output is 7 .


7. Bitwise Operator :

 Bitwise Operators are used to manipulate the individual bit. bit means 0 and 1 .operation perform on bits. In Bitwise operator 1 is for true , 0 is for false.

There are various types of Bitwise  Operator in java .

  • &: Binary AND
  • |  :Binary OR
  • ^ :Binary XOR
  • ~ :Binary one's complement or Binary NOT
  • << :Binary left shift 
  • >> : Binary right shift 
Understand all these operators with examples:

Binary AND : In Binary AND if both bits are true then overall is  true .  

for example  :
calculate x= c & d , what will be the value of x? when c=0101 , d=0110 .
ANSWER : 0 1 0 0 

Binary OR : In Binary OR if any of one bit is true then true.

For example : 
calculate x= c | d , what will be the value of x? when c=0101 , d=0110 .
ANSWER : 0 1 1 1

Binary XOR : In Binary XOR  if the both bits  are  same then false , if both bits are different  then true .

calculate x= c ^ d , what will be the value of x? when c=0101 , d=0110 . 
ANSWER: 0 0 1 1

Binary one's complement or Binary NOT :  In this it give true value for false or false value for true. 

1's complement of 1 = 0 
1's complement of  0 =1
Example: Find the complement of a  if a= 01011
1's Complement of a is 10100  

Binary left shift  << : It shift the bit left side by the value . 

Example : Find A<<1 , if the value of A is 0101.
A<<1  , shift the bits of A left side  by one position .
ANSWER : 1 0 1 0


Binary right shift >>:
  It shift the bit left side by the value . 

Example : Find A>>1 , If the value of A is 0101.
A>>1  , shift the bits of A right  side  by one position . 


ANSWER : 0 0 1 0

Post a Comment

have you any doubt then ask.

Previous Post Next Post