Thursday, September 19, 2013

Operators – [Assignment Operators]



Like keywords, variable, and methods - operators too are integral part of all programming languages and one cannot move an inch without it. Java have the following types of operator.

Assignment Operators / Compound Assignment Operators  
Arithmetic Operators
Relational Operators  
Logical Operators     
Bit wise Operator     
Conditional Operator


Assignment operator(=) used to assign a value to a variable.

int a = 10;

If the varible has an existing value then it will be overwritten by the current one.

Examples :

a = 7; // assigning a litteral
a = a + b; // assigning an an expression

Explicity type casting also used while assigning.

double a=12.566;
int b=(int)a;

Compound Assignment Operators

These operators used while modifying a variable values, that mean the variable should already addigned before. Java have total eleven Compound Assignment Operators.

a=10;
a+=10; // equivalent a=a+10, assign the value of an addition
a-=10;
a*=10;
a/=10;
a%=10;

Following Compound Assignment Operators  used as logical AND, OR, XOR, bitwise left shift, bitwise right shift and  unsigned bitwise right shift :
&=, |= , ^= , <<= , >>=       , >>>=




Examples

public class OperatorDemo_1
{ 
 
    public static void main(String args[])
    { 
       
        int a=10;
        int b=20;
        System.out.println("Assigned values in Variable a and b "+a+","+b); 
        //Assigning String and Character 
        char ch ='x';
        String name="Sachin";
          //Assigning an expression
        int c = a+b; 
        System.out.println("* * Primitive int Assignment  "+ a); 
        //Implicit Casting of variable x and y 
        double d = a; 
        System.out.println("* * Implicit Type Casting int to double "+ d); 
        d=12.45;
        int x=(int)d;
        System.out.println("* * Explicit Type Casting from double to int "+ x); 
        //Compound Assignment Operators 
        x=25;
        System.out.println("\t**Value of x= "+x);
        x+=10;
        System.out.println("\t\tAfter x+=10  "+x);
        x-=10;
        System.out.println("\t\tAfter x-=10  "+x);
        x*=5;
        System.out.println("\t\tAfter x*=5  "+x);
        x/=2;
        System.out.println("\t\tAfter x/=2  "+x);
        x%=2;
        System.out.println("\t\tAfter x%=2  "+x);
       
        x=25;
        System.out.println("\n\t* Value of x= "+x);
        x&=15;
        System.out.println("\t\tAfter bitwise AND x&=15  "+x);
        x|=2;
         System.out.println("\t\tAfter Bitwise OR x|=2  "+x);
        x^=2;
         System.out.println("\t\tAfter Bitwise XOR x^=2  "+x);
        x<<=2;
         System.out.println("\t\tAfter LEFTSHIFT x<<=2  "+x);
        x>>=2;
         System.out.println("\t\tAfter RIGHTSHIFT x>>=2  "+x);
         x=-10;
         System.out.println("\n\t* Value of x= "+x);
        x>>>=2;
        System.out.println("\t\tAfter RIGHTSHIFT x>>=2  "+x);
     
    } 
}



Output

Assigned values in Variable a and b 10,20
* * Primitive int Assignment  10
* * Implicit Type Casting int to double 10.0
* * Explicit Type Casting from double to int 12
          **Value of x= 25
                   After x+=10  35
                   After x-=10  25
                   After x*=5  125
                   After x/=2  62
                   After x%=2  0

          * Value of x= 25
                   After bitwise AND x&=15  9
                   After Bitwise OR x|=2  11
                   After Bitwise XOR x^=2  9
                   After LEFTSHIFT x<<=2  36
                   After RIGHTSHIFT x>>=2  9

          * Value of x= -10
                   After RIGHTSHIFT x>>=2  1073741821

No comments: