Tuesday 30 January 2018

Operators in Java

Who operates between two  operands are called as operators.
Example: a+b
Where a and b are two operands and + is the operator.
 Types
 Java operators can be divided into following categories:
·         Arithmetic operators
·          Unary operators(Increment and Decrement operators)
·         Relational  operators
·         Logical operators
·         Bitwise operators
·         Assignment operators
·         Conditional operators or ternary operators
·         instanceOf operators
 Arithmetic operators
Arithmetic operators are used in mathematical expression and  algebra.
Operator
Description
+
adds two operands
-
subtract second operands from first
*
multiply two operand
/
Calculate quotient
%
remainder of division
++
Increment operator increases integer value by one
--
Decrement operator decreases integer value by one
Example:
public class ArithmeticOperDemo {
   public static void main(String args[]) {
      int num1 = 200;
      int num2 = 20;

      System.out.println("num1 + num2: " + (num1 + num2) );
      System.out.println("num1 - num2: " + (num1 - num2) );
      System.out.println("num1 * num2: " + (num1 * num2) );
      System.out.println("num1 / num2: " + (num1 / num2) );
      System.out.println("num1 % num2: " + (num1 % num2) );
   }
}
Output:
num1 + num2: 220
num1 - num2: 180
num1 * num2: 4000
num1 / num2: 10
num1 % num2: 0

Unary operators(Increment and Decrement operators)
Operator
Description
++
Incremented by 1
--
Decremented by 1

Example:
public class IncreDecreOperatorDemo {
   public static void main(String args[]){
      int num1=200;
      int num2=300;
      num1++;
      num2--;
      System.out.println("num1++ is: "+num1);
      System.out.println("num2-- is: "+num2);
   }
}
Output:
num1++ is: 201
num2-- is: 299
Relational or Comparison operators

Operator
Description
==
Check if two operand are equal
!=
Check if two operand are not equal.
> 
Check if operand on the left is greater than operand on the right
< 
Check operand on the left is smaller than right operand
>=
check left operand is greater than or equal to right operand
<=
Check if operand on left is smaller than or equal to right operand
Example:
public class RelationalOperDemo {
   public static void main(String args[]) {
      int num1 = 20;
      int num2 = 60;
      if (num1==num2) {
             System.out.println("num1 and num2 are equal");
      }
      else{
             System.out.println("num1 and num2 are not equal");
      }

      if( num1 != num2 ){
             System.out.println("num1 and num2 are not equal");
      }
      else{
             System.out.println("num1 and num2 are equal");
      }

      if( num1 > num2 ){
             System.out.println("num1 is greater than num2");
      }
      else{
             System.out.println("num1 is not greater than num2");
      }

      if( num1 >= num2 ){
             System.out.println("num1 is greater than or equal to num2");
      }
      else{
             System.out.println("num1 is less than num2");
      }

      if( num1 < num2 ){
             System.out.println("num1 is less than num2");
      }
      else{
             System.out.println("num1 is not less than num2");
      }

      if( num1 <= num2){
             System.out.println("num1 is less than or equal to num2");
      }
      else{
             System.out.println("num1 is greater than num2");
      }
   }
}
Output:
num1 and num2 are not equal
num1 and num2 are not equal
num1 is not greater than num2
num1 is less than num2
num1 is less than num2
num1 is less than or equal to num2
Logical operators

Operator
Description
Example
&&
Logical AND
(a && b) is false
||
Logical OR
(a || b) is true
!
Logical NOT
(!a) is false
Example:
public class LogicalOperDemo {
   public static void main(String args[]) {
      boolean num1 = true;
      boolean num2 = false;

      System.out.println("num1 && num2: " + (num1&&num2));
      System.out.println("num1 || num2: " + (num1||num2));
      System.out.println("!(num1 && num2): " + !(num1&&num2));
   }
}
Output:
num1 && num2: false
num1 || num2: true
!(num1 && num2): true
Bitwise operators

Operator
Description
&
Bitwise AND
|
Bitwise OR
^
Bitwise exclusive OR
>> 
left shift
<< 
right shift

 truth table 
a
b
a & b
a | b
a ^ b
0
0
0
0
0
0
1
0
1
1
1
0
0
1
1
1
1
1
1
0
The bitwise shift operators shifts the bit value. The left operand specifies the value to be shifted and the right operand specifies the number of positions that the bits in the value are to be shifted. Both operands have the same precedence.
Example:
public class BitwiseOperDemo {
  public static void main(String args[]) {

     int num1 = 13; 
     int num2 = 26; 
     int result = 0;

     result = num1 & num2;  
     System.out.println("num1 & num2: "+result);

     result = num1 | num2;  
     System.out.println("num1 | num2: "+result);
   
     result = num1 ^ num2;  
     System.out.println("num1 ^ num2: "+result);
   
     result = ~num1;  
     System.out.println("~num1: "+result);
   
     result = num1 << 2;  
     System.out.println("num1 << 2: "+result); result = num1 >> 2;  
     System.out.println("num1 >> 2: "+result);
  }
}
Output:
num1 & num2: 8
num1 | num2: 31
num1 ^ num2: 23
~num1: -14
num1 << 2: 52
num1 >> 2: 3
Assignment Operators

Operator
Description
Example
=
assigns values from right side operands to left side operand
a = b
+=
adds right operand to the left operand and assign the result to left
a+=b is same as a=a+b
-=
subtracts right operand from the left operand and assign the result to left operand
a-=b is same as a=a-b
*=
multiply left operand with the right operand and assign the result to left operand
a*=b is same as a=a*b
/=
divides left operand with the right operand and assign the result to left operand
a/=b is same as a=a/b
%=
calculate modulus using two operands and assign the result to left operand
a%=b is same as a=a%b
Example:
public class AssignmentOperDemo {
   public static void main(String args[]) {
      int num1 = 30;
      int num2 = 10;

      num2 = num1;
      System.out.println("= result: "+num2);

      num2 += num1;
      System.out.println("+= result: "+num2);
                 
      num2 -= num1;
      System.out.println("-= result: "+num2);
                 
      num2 *= num1;
      System.out.println("*= result: "+num2);
                 
      num2 /= num1;
      System.out.println("/=result: "+num2);
                 
      num2 %= num1;
      System.out.println("%= result: "+num2);
   }
}
Output:
= result: 30
+= result: 60
-= result: 30
*= result: 900
/=result: 30
%= result: 0



Conditional operator or ternary operator(?:)
It is also known as ternary operator and used to evaluate Boolean expression,
exprcond1?expr2:expr3
If exprcond1 Condition is true? Then value expr2 : Otherwise value expr3

Example:
public class TernaryOperDemo {

   public static void main(String args[]) {
        int num1, num2;
        num1 = 30;
      
            num2 = (num1 == 30) ? 100: 200;
            System.out.println( "num2: "+num2);


            num2 = (num1 == 15) ? 100: 200;
            System.out.println( "num2: "+num2);
   }
}

Output:
num2: 100
num2: 200
instanceOf operator
The operator checks whether the object is of particular type (class type or interface type).
This operator is used for object reference variables.
It returns either true or false. It returns true if the object is of particular type (class type or interface type) otherwise false
Example:
public class Demo{ 
 public static void main(String args[]){ 
 Demo d1=new Demo(); 
 System.out.println(d1 instanceof Demo);
 } 
Output:
true
Operator Precedence in Java
It determines which operator needs to be evaluated first if an expression has more than one operator. Operator with higher precedence at the top and lower precedence at the bottom.

Operators

Precedence

postfix increment and decrement
++ --
prefix increment and decrement, and unary
++ -- + - ~ !
multiplicative
* / %
additive
+ -
shift
<< >> >>>
relational
< > <= >= instanceof
equality
== !=
bitwise AND
&
bitwise exclusive OR
^
bitwise inclusive OR
|
logical AND
&&
logical OR
||
ternary
? :
assignment
= += -= *= /= %= &= ^= |= <<= >>=>>>=




No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...