Saturday, November 16, 2013

ජාවා operators...


Control statement බාවිතා කරන්න කලින් අපි operators ගැන ටිකක් දැනගන්න ඕනේ.ජාවා වල ප්‍රදාන වශයෙන්  operators වර්ග පහක්  තියනවා එව තමය්
·         Arithmetic Operators

·         Relational Operators

·         Bitwise Operators
·         Logical Operators
·         Assignment Operators



Arithmetic Operators
මේවා තමය් සාමාන්‍ය ගණිත කර්ම කරන කොට පාවිචි කරන්නේ.අපි සමහර operators කලින් programme වෙලත් මේවා බාවිතා කරා අලුත් දෙයක් නැ.පහත වගුව බලන්න
A=10
B=20

Operator
Description
Example
+
Addition - Adds values on either side of the operator
A + B will give 30
-
Subtraction - Subtracts right hand operand from left hand operand
A - B will give -10
*
Multiplication - Multiplies values on either side of the operator
A * B will give 200
/
Division - Divides left hand operand by right hand operand
B / A will give 2
%
Modulus - Divides left hand operand by right hand operand and returns remainder
B % A will give 0
++
Increment - Increases the value of operand by 1
B++ gives 21
--
Decrement - Decreases the value of operand by 1
B-- gives 19
 
ඕනේ නම් අන්තිම තුන තරුම් ගන්න පහත programme බලන්න
import java.util.Scanner;//line 1
class Example5{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in); //line 2
       
        System.out.println("Enter an integer : ");
        int input = sc.nextInt(); //Line 3
       
        int mod=input%5;
        System.out.println(input+"%"+5+"="+mod);

    }
}

මේකේ මන් ගත්තු output එක මෙන්න
 
Enter an integer :

62

62%5=2


ඔයාලට ප්‍රශ්නයක් ඇති 62 ආවේ කොහොමද කියල.එක කරේ මන් ගත්ත user ඉන්පුට් එකක් මුලින්ම line 1 වල වගේ අවශ paccage එක ඉම්පෝර්ට් කරගන්න ඕනේ.ඊ ට පස්සේ sc කියල object එකක් හදනවා.අන්තිමට line 3 වගේ ඉන්පුට් එක අරන් verable එකකට දාගන්නවා.මන් මෙහෙම කීවට ඔයාලට තෙරන්නේ නැතුව ඇති දැනට එක මතක තියාගන්න.OOP පාඩමේදී ඒ ගැන හරියටම තේරවි දැනට user ඉන්පුට් එකක් ගන්න ඒ ක්‍රමේ මතක තියාගන්න int input එකකට sc.nextInt() ,  Short input එකකට  sc.nextShort() වගේ අදාල data type එකේ නම Capital මුල් අකුරක් එක්ක පාවිචිචි කරන්න.% ලකුණින් වෙන්නේ බෙදල ඉතුරු ගාන ගන්න එක.ඔයාලට 6262%5=2 මෙකින් වනු දේ පේනවා ඇති.දැන් අපි තව programme එකක් බලමු.
 

class Example5{
      public static void main(String args[]){
            int input=10;
            int input1=10;
            int input2=10;
            int input3=10;
       
        int inc=input++;
        System.out.println("10++"+"="+inc);
        int inc1=++input1;
        System.out.println("++10"+"="+inc1);
       
        int dic=input2--;
        System.out.println("10--"+"="+dic);
        int dic1=--input3;
        System.out.println("--10"+"="+dic1);
      }
}

මේකේ output එක තමය් පහත තියනේ
10++=10
++10=11
10--=10
--10=9

පේනවා ඇති වෙනස.නමුත් මේ හැම අවස්තවදීම එකක් අඩු හෝ වැඩි වෙලා තියනවා නමුත් පෙන්නේ නේ දෙකක විතරය් අනිත්දෙක අගයන් පරණ අගයන් වනවා.නමුත් input කියන verable වල අගය වෙනස් වෙලා තියනේ .පහත programme එක බලමු.
 

class Example5{
      public static void main(String args[]){
            int input=10;
            int input1=10;
            int input2=10;
            int input3=10;
       
        int inc=input++;
        System.out.println("10++"+"="+inc);
        System.out.println("input"+"="+input);
        int inc1=++input1;
        System.out.println("++10"+"="+inc1);
        System.out.println("input1"+"="+input1);
        int dic=input2--;
        System.out.println("10--"+"="+dic);
        System.out.println("input2"+"="+input2);
        int dic1=--input3;
        System.out.println("--10"+"="+dic1);
        System.out.println("input3"+"="+input3);
      }
}
Out put එක මෙන්න
10++=10
input=11
++10=11
input1=11
10--=10
input2=9
--10=9
input3=9
මේකේ වන දේ පටල ගන්නේ නැතුව තරුම් ගන්න බලන්න අපිට මේක එදිරියට ඕනේ වනවා.

 Relational Operators

දැන් බලමු Relational Operators කියන්නේ මොනවාද කියල
Operator
Description
Example
==
Checks if the values of two operands are equal or not, if yes then condition becomes true.
(A == B) is not true.
!=
Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.
(A != B) is true.
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
(A > B) is not true.
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
(A < B) is true.
>=
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
(A >= B) is not true.
<=
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.
(A <= B) is true.
 
මේවා තමය් Control statement වල use කරන්නේ පහත උදාහරණයක් බලන්න

if(condition){

     //Do Something
}else{
     //Do Something
}

Condition කියන තැනේදී තමය් use කරන්නේ.වැඩිදුර Control statement කරනකොට බලමු.


Logical Operators
දැන් අපි බලමු Logical Operators කියන්නේ මොනවාද කියල

Operator
Description
Example
&&
Called Logical AND operator. If both the operands are non-zero, then the condition becomes true.
(A && B) is false.
||
Called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true.
(A || B) is true.
!
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.
!(A && B) is true.
 
Condition කියන තැනේදී තමය් මේවත්  use කරන්නේ.වැඩිදුර Control statement කරනකොට බලමු.


Assignment Operators 

Operator
Description
Example
=
Simple assignment operator, Assigns values from right side operands to left side operand
C = A + B will assign value of A + B 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 =


 
මේකේ වන දේ පැහදිලි කරගන්න මන් programme එකක් දේනම් .පහත programme එක බලන්න්න.
class Example5{
      public static void main(String args[]){
            int input=10;
            int input1=10;
       
        input=input+10;//line 1
        input1 +=10;//line 2
        System.out.println("input"+"="+input);
        System.out.println("input1"+"="+input1);
      }
}

Line 1 ,line 2 වන වැඩ දෙකම සමාන වනවා.අනිත් operators වලත් මේවගේම වැඩ කරනවා.ඒවාට ඔයගොල්ලෝ programme ටික්කක් ලියන්න බල්ලන.අමාරු නැ..

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

Precedence of Java Operators(expression එකකදී operators execute වන පිලිවල )
Category 
Operator 
Associativity 
Postfix 
() [] . (dot operator)
Left to right 
Unary 
++ - - ! ~
Right to left 
Multiplicative  
* / % 
Left to right 
Additive  
+ - 
Left to right 
Shift  
>> >>> <<  
Left to right 
Relational  
> >= < <=  
Left to right 
Equality  
== != 
Left to right 
Bitwise AND 
Left to right 
Bitwise XOR 
Left to right 
Bitwise OR 
Left to right 
Logical AND 
&& 
Left to right 
Logical OR 
|| 
Left to right 
Conditional 
?: 
Right to left 
Assignment 
= += -= *= /= %= >>= <<= &= ^= |= 
Right to left 
Comma 
Left to right 
   
අපි ඊළඟ පොස්ට් එකින් Control statementගැනකතාකරමු...
 

No comments: