Thursday, May 7, 2015

Lesson 06


ඉතින් කොහොමද යාලුවනේ ඔන්න අදත් මම ඔයාලට තවත් අලුත් පොස්ටුවක් අරගෙන අවා අද අපිට කතා කරන්න තියෙන්නේ අපි එදා අන්තිමට කතා කරලා ඉවර කරපු ඔපෙරටෝර්ස් වලම කොටසක් තමයි.

 Assignment Operators සහ Misc Operators ගැන තමි මම අද කියල දෙන්න යන්නේ.එහෙනම් අපි මුලින් ම බලමු මොනවද මේ assignment Operators කියල අපිට මේ යටතේ ඔපෙරේටර්ස්  කිහිපයක් ම හම්බෙනවා අපි බලමු එහෙනම් ඒ මොනවද කියල.



Operator
Description
Example

=
 Simple assignment operator, Assigns values from right side operands to left side operand

C = X + Y will assign
 value of X + Y into C


+=
 Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand

C += A is equivalent to C = C + A


-=
 Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand

C -= A is equivalent to C = C - A


*=
 Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand

C *= A is equivalent to C = C * A


/=
Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand

C /= A is equivalent to C = C / A


%=
Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand

C %= A is equivalent to
 C = C % A

<<=
Left shift AND assignment operator

C <<= 2 is same as C = C << 2

>>=
Right shift AND assignment operator

C >>= 2 is same as C = C >> 2

&=
Bitwise AND assignment operator

C &= 2 is same as C = C & 2

^=
bitwise exclusive OR and assignment operator

C ^= 2 is same as C = C ^ 2

|=
bitwise inclusive OR and assignment operator

C |= 2 is same as C = C | 2


















































 මේ ටික ඉතාමත් සරල operators ටිඅකක් හින්දා මම වැඩිය විස්තර කරන්න යන්නේ නෑ මේක කියෙව්වහමත් ඔයාලට තේරෙයි කොහොමද කරන්නේ කියල එක නිසා මම මේ ඔක්කොටම example එකක් කරන්නම්.



public class AssignmentOpr {

    public static void main(String args[]) {

        int a = 50;
        
        int b = 100;
        
        int c = 0;

        c = a + b;
        
        System.out.println("c = a + b = " + c);

        c += a;
        
        System.out.println("c += a  = " + c);

        c -= a;
        
        System.out.println("c -= a = " + c);

        c *= a;
        
        System.out.println("c *= a = " + c);

        a = 100;
        
        c = 50;
        
        c /= a;
        
        System.out.println("c /= a = " + c);

        a = 20;
        
        c = 15;
        
        c %= a;
        
        System.out.println("c %= a  = " + c);

        c <<= 2;
        
        System.out.println("c <<= 2 = " + c);

        c >>= 2;
        
        System.out.println("c >>= 2 = " + c);

        c >>= 2;
        
        System.out.println("c >>= a = " + c);

        c &= a;
        
        System.out.println("c &= 2  = " + c);

        c ^= a;
        
        System.out.println("c ^= a   = " + c);

        c |= a;
        
        System.out.println("c |= a   = " + c);
        
    }

}




 ඔයාලට result එක එන්න ඕනේ මෙන්න මේ විදියට තමයි

c = a + b = 150

c += a  = 200


c -= a = 150


c *= a = 7500


c /= a = 0


c %= a  = 15


c <<= 2 = 60


c >>= 2 = 15


c >>= a = 3


c &= 2  = 0


c ^= a   = 20


c |= a   = 20


 ඊටපස්සේ එහෙනම් අපි බලමු මොනවද මේ Misc Operators කියන්නේ කියල

මේ යටතට අපිට operators දෙකක් ගන්න පුළුවන් ඒ තමයි

  • Conditional Operator ( ? : )

  • instanceof Operator
 මුලින්ම බලමු එහෙනම් Conditional Operator එක ගැන,

 මේකට අපි ternary operator කියලත් කියනවා මේකෙදි  boolean expression එකක් evaluate කරන්න operands 3ක් use කරනවා.

බලන්නකෝ syntax එක,

variable x = (expression) ? value if true : value if false

 එහෙනම් අපි මේකට example එකක් කරලා බලමු 




public class ConditionalOpr {

   public static void main(String args[]){
       
      int x , y;
      
      x = 10;
      
      y = (x == 1) ? 50: 100;
      
      System.out.println( "Value of y is : " +  y );

      y = (x == 10) ? 50: 100;
      
      System.out.println( "Value of y is : " + y );
      
   }
   
}






  • Value of y is : 100
  • Value of y is : 50


 මේකෙදි වෙන්නේ පලවෙනි statement එකේදී x ගේ value එක එකට සමානද කියල බලනවා  එක false නිසා අපිට එන්නේ 100 කියන value එකයි ඊලග  statement එකේදී අපි බලන්නේ value එක 10 ට සමානද කියලයි එක true වෙන නිසා අපිට 50 කියන උත්තරය ලැබෙනවා


ඊලගට අපි බලමු එහෙනම් instanceof Operator එක ගැන,
මේ operator එක අපි use කරන්නේ object reference variables වලට විතරයි. මේ operator එකෙන් චෙක් කරනවා object එක class type or interface type ද කියල,

Syntax එක බලාගන්නකෝ එහෙනම්,

( Object reference variable ) instanceof  (class/interface type)



public class InstanceofOpr {

   public static void main(String args[]){
       
      String name = "Amal";
      
      // following will return true since name is type of String
      
      boolean result = name instanceof String;  
      
      System.out.println( result );
      
   }
   
}




මේකේ result එක විදියට එන්නේ 

True කියල

එක වෙන්නේ instanceof operator එකෙන් චෙච්ක් කරනවා name කියන variable එකේ තියෙන්නේ string type එකේ value එකක් ද කියල.


මෙතනින් අපි කරපු operators පාඩම ඉවර වෙනවා එහෙනම් ඉතින් මේ ටික කස්ටිය එක්ක හොදට බලාගන්න අපිට ඉස්සරහට මේ කරපු ඒවා ගොඩක් වැදගත් වෙනවා.

එහෙනම් ඉතින් අදට යන්න වෙලාව හරි අයෙත් තවත් අලුත් පොස්ටුවකුත් අරගෙන එන්නම් එතකම් හැමෝටම ජයවේවා!!!


 මතක ඇතුව කොමෙන්ටුවකුත් දාල යන්න යාලුවනේ.....







No comments: