operators (operator part 3)

Relational operators

අද කතකාරන්න යන්නේ relational හා ternary operators ගෙන. මේ relational operators වල ප්‍රධාන කාර්ය වෙන්නේ numbers දෙකක් එහෙමත් නැත්නම් අගයන් දෙකක් සංසන්දනය (compare) කරලා Boolean අගයක් (true or false) return කරන එක. ජාවා වල මේ type එකේ operators හයක් තියනවා.

<    less than                                         ( x < y ,  x කුඩයි y ට වඩා).
>    greater than                                    ( x > y ,  x විශාලයි y ට වඩා).
<= less than or equal                      ( x <= y ,  x කුඩයි හෝ සමානයි y ).
>= greater than or equal                ( x >= y , x විශාලයි හෝ සමානයි y ).
== equal                                         ( x == y , x සමානයි y ).
!=   not equal                                  ( x != y , x අසමානයි y ).


මේ operators ටික නම් බැලූ බැල්මටම ලේසි තේරුම් ගන්න. මේ operators හයටම අපි එක program එකක් බලමු.  ඊට පස්සෙ ටිකක් කතාකරමු.

public static void main(String[] args) {
        int x = 10;
        int y = 15;
        System.out.println("x less than y :" + (x < y));
        System.out.println("x greater than y :" + (x > y));
        System.out.println("x less than or equal to y :" + (x <= y));
        System.out.println("x greater than or equal to y :" + (x >= y));
        System.out.println("x equal to y :" + (x == y));
        System.out.println("x not equal to y :" + (x != y));
    }

ඔයාලට පෙන්නව මේ program එකේ මම x වල අගය විදිහට දීල තියෙන්නේ 10 ඒ වගේම y වල අගය දීල තියෙන්නේ 15. අපි මේක relational operators භාවිතාකරලා කියනවනම් මෙහෙමයි ( x < y ) x කුඩයි y ට වඩා. මේක තමා එතකොට හරි condition එක. මම දීල තියන program එකේ මේ condition එක භාවිතාකරලා තියෙන්නේ පලවෙනි print line එකේ.

“ System.out.println("x less than y :" + (x < y)); “

බැලූ බැල්මට එහෙම පෙනුනට මේ program එකේ condition තුනක් හරි. අපි බලමු ඒ මොනවද කියලා.

1. System.out.println("x less than y :" + (x < y));
2. System.out.println("x not equal to y :" + (x != y));
3. System.out.println("x less than or equal to y :" + (x <= y));


මේ line තුනම හොදට බලන්න. එක, x කුඩයි y ට වඩා. දෙක, x අසමානයි y ට. තුන, x කුඩයි හෝ සමානයි y ට. එහෙනම් මේ condition තුනම හරි.  මෙන්න output එක.


මේ program එක run කරල බලන්න උඩින් කියපු condition තුන තියන lines වලට අදාලව ture කියල එයි ඉතුරු ඔක්කොම lines වලට අදාලව false print වෙයි. අපි තවත් program එකක්  if else use කරලා බලමු තේරුම්ගන්න ලේසි වෙන්න.

public static void main(String[] args) {
        int x = 10;
        int y = 15;

        if (x == y) {
            System.out.println("X equal to Y.");
        } else if (x > y) {
            System.out.println("X greater than Y.");
        } else if (x <= y) {
            System.out.println("X less than or equal to Y");
        } else if (x >= y) {
            System.out.println("X greater than or equal to Y");
        } else if (x != y) {
            System.out.println("X not equal to Y");
        } else if (x < y) {
            System.out.println("X less than Y.");
        }
    }

If else දන්නැත්නම් ඒකට අවුල්ක් ගන්න එපා. මේකෙන් කරන්නේ if , else if එකේ වරහන් ඇතුලේ තියන condition එක check කරලා output එකක් දෙන එක. මේක වැඩකරන්නේ මෙහෙමයි. පලවෙනියටම if එක ඇතුලෙ තියන condition එක check කරනවා. ඒක වැරදිනම් ඊලගට තියන else if එක ඇතුලට ගිහින් එකේ තියන condition එක check කරනවා. එකත් වැරදිනම් ඊලගට තියන else if එකේ condition එක check කරනවා. හැබැයි if එකෙ condition එක හරිනම් ඒක ඇතුලට ගිහින් එකේ තියන "X equal to Y." කියන line එක print කරනවා. එතනින්  ඉවරයි. එක condition එකක් වැරදිනම් විතරයි අනිත් එකට යන්නේ. Output එක මෙන්න.


ඉතින් මේ program එකේ තුන් වෙනියට තියන condition එක බලන්න. ඒක තමා පලවෙනියටම true වෙන condition එක. ඉතින් ඒ line එක print කරලා program එක නතර වෙනවා. Program එක compile කරලා බලන්න. If else ගෙන වැඩිය කතාකරන්නේ නෑ ඒක වෙනම අපි කතා  කරමු.

Ternary Operator

අපි ternary operator විදියට operators දෙකක් හදුනගත්තා පලවෙනි lesson එකේදී. ( ? , : ) මේ operator දෙක තමා java වල ternary operator වෙන්නේ. දැන් අපි බලමු ඒවා කොහොමද භාවිතා වෙන්නේ කියලා. මේ operator දෙකම අපිට if else වෙනුවට භාවිතා කරන්නපුලුවන්. අපි බලමු කොහොමද ඒක වෙන්නේ කියලා. If else වැඩකරනහැටි උඩ විස්තරේදී පොඩ්ඩක් දැන ගත්ත නිසා මේ program එක බලන්න.

public static void main(String[] args) {
        int number = -5;
        if (number > 0) {
            System.out.println(number + " is positive");
        } else {
            System.out.println(number + " is not positive");
        }
}

int variable එකක් තියනවා number කියලා අපි එකට -5 කියන අගය සමාන කරලා තියනවා. If condition එක ඇතුලෙදි number variable එකේ අගය 0 ට වඩා වැඩීනම් ඒක positive අගයක් ලෙසත්, එහෙම නැත්නම් ඒ අගය positive  නොවන අගයක් ලෙසත් print කරන්න කියල තමා මේ program එකෙන් code කරලා තියෙන්නේ.  දැන් අපි බලමු ternary operators භාවිතාකරලා කොහොමද මේ program එක code කරන්නේ කියලා.  

public static void main(String[] args) {
        int number = -5;
  String result;
result = (number > 0) ? "positive" : "not positive";
        System.out.println(number + " is " + result);}

මේ program එකේත් උඩින් එකේ තිබුන int variable එකයි, තව string type එකේ variable එකක් තියනවා result කියන name එකෙන්. මේ String කියන්නේ character values store කරලා තියගන්න පුලුවන් data type එකක් කියල දැනට මතක තියාගන්න.  මේ program එක කියවන්නේ මෙහෙමයි.

number කියන variable එකෙ තියන අගය බිංදුවට වඩා විශාලයිනම් result කියන String variable එකට සමාන කරන්නලු “positive” කියලා. එහෙම නැත්නම් සමාන කරන්නලු “not positive” කියලා. මේ program එක අරගෙන run කරලා බලන්න. If else එක භාවිතා කරල කරපුදේමයි මේකෙනුත් කරල හියෙන්නේ. “?” (question mark) එකෙන් if condition එක ඇතුලෙදි කරන දේත් “:” (colon) එකෙන් else part එකත් සම්පූර්න වෙනවා. “for each” loop එකට එහෙමත් මේ operator එක භාවිතා වෙනවා.

System.out.println(number + " is " + result);

මේ line එකේදී කරලා තියෙන්නේ number කියන variable එකයි, “is” කියන word එකයි, result කියන variable එකයි print කරනඑක. Plus mark (+) එකෙන් කරලා තියෙන්නේ මේ  අගයන්,ඒ වගේම is කියන word එක එකිනෙකට සම්බන්ධ කරනඑක. Variable එකක් print කර ගන්න ඕනිනම් double quotes ("")  භාවිතා නොකර නිකම්ම print කරගන්නවා මේ  විදිහට.

System.out.println(number);

Variable දෙකක් හෝ ඊට වඩා print කරගන්න ඕනිනම් මෙහෙමයි.

System.out.println(number + result); 
// System.out.println(number + result + number);

හැබැයි අපිට වෙන මොකක් හරි වචනයක්, වාක්‍යක් මේ අතරට දාගන්න ඕනිනම් වරහන් භාවිතා කරන්න වෙනවා. මේ වගේ.

System.out.println(number + " is " + result);

ඔන්න ඒ ටිකත් එතනදිම කිව්වා එහෙනම් තවත් පාඩමකින් හම්බෙමු. 
ස්තූතියී.....

ජාවා operators (2 කොටස)

අද කතාකරන්න යන්නේ logical operators ගැන.


&&      =  logical AND operator
||           = logical OR operator
!           = logical NOT operator


ජාවා වලදී ප්‍රධානවශයෙන් logical operators තුනක් භාවිතාවෙනවා. මේ operators තුනම බොහෝ  දුරට භවිතාවෙන්නේ conditions check කිරීමේදීයි. 

අපි && (AND) operator එක අරන් බලමු. 

මේකෙන් කරන්නේ condition දෙකක් පරීක්ෂා කරනඑකයි. අපි check කරන condition දෙකෙන් එකක් හරි false නම් අපිට උත්තරේ දෙන්නේ false කියලා. අපි මේකට උදාහරණයක් බලමු.

public static void main(String[] args) {
       int x=10;
       int y=5;
       int z=15;     
       System.out.println(y<x && x>z);
       System.out.println(y<x && z>x);     
    }

මේ program එක බලමු.

X අගය 10 යි, Y අගය 5යි,  Z අගය 15 යි. ඊට පස්සේ එක line එකක් print කරනවා. ඒකෙදී condition දෙකක්  check කරනවා && (AND) operator එක මගින්. පලවෙනි condition එක වෙන්නේ Y කුඩයි  X ට වඩා ( Y<X ) condition එකනම් හරි. AND (&&) operator එක තියන නිසා ඊලග condition එකත් check කරනවා. දෙවෙනි condition එක වෙන්නේ ( X>Z ) X විශාලයි Z ට වඩා. ඒකනම් වැරදියී මොකද X අගය 10 යි, Z අගය 15 යි එහෙනම් X විශාල වෙන්න විදිහක්  නෑ.

එහෙනම් පලවෙනි print line එකේ output එක වෙන්නේ FALSE. හේතුව තමා condition දෙකෙන් එකක් වැරදි වීම.
දෙවෙනි print line එකත් හොදට බලන්න. එකේ condition දෙකම හරි ඒ කියන්නේ output එක TRUE.

මේ program එකත් run කරලා බලන්න.

public static void main(String[] args) {
       int x = 10;
       int y = 5;
       int z = 15;     
       if(x < z && x < y){
        System.out.println("Both conditions are true");
       }else{
        System.out.println("FALSE");
         }
       }

ඊලගට අපි || (OR) operator එක බලමු.

මේ operator එකෙන් කරන්නෙත් condition දෙකක් check කරන එක. Check කරන condition  දෙකෙන් එකක් හරි හරිනම් output එක වෙන්නේ TRUE. අපි කලින් ගත්ත program එකම අරගෙන බලමු.

public static void main(String[] args) {
        int x = 10;
        int y = 5;
        int z = 15;
        System.out.println(y < x || x > z);
        System.out.println(y > x || z < x);
    }

පලවෙනි print line එක කියවන්නේ මෙහෙමයි y කුඩයි x ට වඩා හෝ x විශාලයි z ට වඩා”. ඉතින් මේ condition දෙකෙන් දෙවෙනි condition එක වැරදි නමුත් එක condition එකක් හරි නිසා මේකේ output එක වෙන්නේ TRUE. මොකද මේකේ හෝ  කියලා තියනවා පලවෙනි එක නැත්නම් දෙවෙනි එක. දෙවෙනි print line එකේ condition දෙකම වැරදි ඒ නිසා output එක වෙන්නේ FALSE.

මේ program එකත් check කරලා බලන්න.

public static void main(String[] args) {
        int x = 10;
        int y = 5;
        int z = 15;
if(x<z || x<y){
        System.out.println("TRUE");
       }else {
        System.out.println("FALSE");
       }
}

! (NOT) operator

අගයන් දෙකක් එකිනෙකට අසමානයි නම් ඒ අසමානතාවය අගයන් දෙක සංසන්දනය කර එය සත්‍ය හෝ අසත්‍ය බව පෙන්නුම් කිරීමට මේ operator එක භාවිතා කරනවා.   

public static void main(String[] args) {
       int x,y,z;
       x=10;
       y=5;
       z=15;
       System.out.println(x>z);
       System.out.println(!(x>z));
    }

මේ program එක අපි බලමු. කලින් program වල තිබුනු අගයන්මයි මේ program එකෙත් තියෙන්නේ. මේකේ පලවෙනි print line එකේ දීල තියන condition එක false. මොකද x අගය z අගයට වඩා කුඩා නිසා. දෙවෙනි condition එකත් ඒ විදිහමයි. ඒ උනාට ඒකට අපි ! (not) operator එක භාවිතා කරලාතියනවා. ඒ නිසා එකේ output එක true වෙනවා. හේතුව තමයි ! (not) operator එක මගින් දීලාතියන condition එක මොකක් උනත් ඒක අනිත්පැත්ත හරවනවා. True එකක් තිබුනොත් false කරනවා. False එකක් තිබුනොත් true කරනවා. අපි තවත් program එකක් බලමු. 

public static void main(String[] args) {
        int x, y, z;
        x = 10;
        y = 5;
        z = 15;
        System.out.println(x > z);
        System.out.println(!(x > z));
        if (z > x && z > y) {
            System.out.println("Z is the largest value.");
        }
        if (x == y || y == z) {
            System.out.println("There are same values.");
        } else {
            System.out.println("There are no similar numbers.");
        }
        if (!(x < y)) {
            System.out.println("x is greater than y. ");
        }
    }

මේ program එක code කරලා run කරල බලන්න. Values වගේම operators මාරුකරලා run කරලත් බලන්න.

Login system එකක database එකෙන් ගන්න user name, password check කරනකොට මේ && (AND) operator එක භාවිතා වෙනවා. ඒ වගේම තමයි system එකක user level පරීක්ෂා කරනකොට ! (NOT) operator එක භවිතාවෙනවා. ඒ වගේම තමා condition කිහිපයක්  භවිතාවෙන තැනක || (OR) operator භවිතා වෙනවා. උදාහරණයක් විදිහට ගත්තොත් system එකක එකම feature එකක් user level දෙකකට පොදුයිනම් ඒ user level දෙක check කිරීමට මේ || (OR) operator එක භාවිතා කරන්න පුලුවන්.  

  • ·       මේ logical operators නිතරම condition එකක් check කරලා එලියට දෙන්නේ Boolean අගයක්, ඒ කියන්නේ true හෝ false කියන දෙකෙන් එකක්.

  • ·       ! (not operator) මේ operator එක සමහර තැන්වලදී unary operator එකක් විදිහටත් හදුන්වනවා. අපි ඒ ගෙන වෙනම කතාකරමු.


එහෙනම් ස්තූතියි ඈ…

ජාවා operators (1 කොටස)


අද කතා කරන්න යන්නේ ජාවා operators ගැන. කවුරුත් දන්නවා operators නැතුව programming කරන්නනම් බැරි බව. Operators කියන්නේ java වල විතරක් නෙවෙයි ඕනිම පරිගනක භාෂාවක අත්‍යවශ්‍ය අංගයක්. අපි කෙලින්ම note එකට යමු. ජාවා වල operators කොටස් කිහිපයකටම බෙදෙනවා. අපි බලමු ඒ මොනවද කියලා.

Category
Operators
Arithmetic
+, -, *, %, ++, --, /,  
Logical operator
&&, ||, !
Relational operator
<, >, <=, >=, ==, !=
Assignment operator
=, +=. -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>=
Shit operator
<<, >>, >>>
Ternary operator
?, :
Bitwise operator
&, ^, |
  
මේ විදිහට ජාවා operators වර්ග කරනවා. අද බලපොරොත්තු වෙන්නේ programming වලදී ගොඩක්ම භාවිතාවන operators වර්ග කීපයක් ගැන.

Arithmetic operator.

මේ arithmetical operators ඕනි වෙන්නේ ගණිතමය සංයුතියක් තියන programming part එකක් coding කිරීමේදියි. මම මෙහෙම දෙයක් කරන්නම් තේරුම් ගන්න ලේසි විදිහකට මේ operators ටික table එකකට උදාහරණත් එක්කම පෙන්නලා දෙන්නම්. මේකට ඕනිවෙනවා integer type එකේ අගයන් දෙකක්. මම int X = 10 ලෙසත් int Y=5 ලෙසත් ගන්නම්. දැන් මේ table එක හොඳට අධ්‍යයන කරන්න.

Operator
විස්තරය
උදාහරණය
+ (Addition)
අගයන් දෙකක් එකතු කිරීමට මෙම operator  එක භාවිතා කරනවා.
X+Y = 15
           -     ( Subtraction)
අගයන් දෙකක් අඩු කිරීමට මෙම operator  එක භාවිතා කරනවා.
X-Y=5
(Multiplication)
අගයන් ගුණ කිරීම සඳහා මෙම operator එක භාවිතා කරනවා.
X*Y=50
/  (Division)
අගයන් බෙදීම සඳහා මෙම operator එක භාවිතා කරනවා.
X/Y=2
% (Module)
යම් අගයක් තවත් අගයකින් බෙදා ඉතිරිය ලබාගැනීමට මෙමෙ operator එක භාවිතා කරනවා.
X%Y=0
++  (increment)
මේ operator එකෙන් කරන්නේ තියන අගයට එකක් එකතු කරන එක. **
X++ = 11
-- (Decrement)
මේ operator එකෙන් කරන්නේ තියන අගයෙන් එකක් අඩු කරන එක. **
X-- = 9

අපි දැන් මේ operators භවිතා කරලා sample program එකක් බලමු.

 public class BasicOperatorsOne {
    public static void main(String[] args) {
        int x = 10;
        int y = 5;
        System.out.println(x+y); 
    }
}

Output: 15


මේ program එක code කරලා  run කරලා බලන්න. අනිත් operators add කරලත් (x-y, x%y, x/y ආදී වශයෙන්) run කරල බලන්න.

** මේ increment සහ decrement operators වල කොටස් දෙකක් තියනවා.

Increment :
  •         Post increment operator (10++)   (අගයට පසු post operator භාවිතා වේ.)
  •         pre increment operator (++10)   (අගයට පෙර pre operator භාවිතා වේ.)



 public class BasicOperatorsOne {
    public static void main(String[] args) { 
        int x = 10;
        int y = 5; 
        System.out.println(++x); //try y++, x++, ++x        
    }
}

Output: 11


උඩ දීලා තියන program එකට (post increment) x++ හා y++ දාල run කරලා බැලුවොත් පිලිතුරේ වෙනසක් නැතිවෙයි. දැන් අපි බලමු එකට හේතුව මොකක්ද කියලා. Post increment operators යම් අගයකට එකතු කරහම එක වැඩ කරන්නේ  දෙවෙනි වතාවට ඒ operator එක තියන line එක run වෙනකොටයි.  මේකටත් අපි උදාහරණයක් බලමු.


 public class BasicOperatorsOne {

    public static void main(String[] args) {

for(int i =0; i > 10 ; i++)
        System.out.println(i);
       
    }
}

Output: 0,1,2,3,4,5,6,7,8,9


මේ program එකේ තියෙන්නෙ for loop එකක්. ඒ මොකක්ද අපි පස්සේ බලමු. දැන් (i++) කියල තියන operator එකට අවධානය යොමු කරන්න. මේ Program එක run කරලා බැලුවොත් out put එක විදිහට 0,1,2,3,4,5,6,7,8,9 පිලිවලින් print  වෙයි. මේ program එකෙන් බලගන්න පුලුවන් post increment එක භවිතාකරලා එකින් එක වැඩි වෙලා දීලා තියන condition (i>10) එක false වෙනකල් වැඩකරලා තියන හැටි. ඉතින් මේකෙන් තේරුම් ගන්න ඕනි දේ තමයි pre increment operator (++x) භාවිතා කරහම පලවෙනි පාර program එක  run වෙනකොටම එකක් එකතු වෙන බවත්. Post increment operator (x++) භවිතා කරහම දෙවෙනි වතාවට post increment operator එක තියන line එක run වෙනකොට එකක් එකතු වෙන බවත්.  


Decrement:
  •          Post decrement operator (10--)    (අගයට පසු operator භාවිතා වේ.)
  •          Pre decrement operator (--10)    (අගයට පෙර operator භාවිතා වේ.)



public class BasicOperatorsOne {

    public static void main(String[] args) {

        int x = 10;
        int y = 5;

        System.out.println(--y); //try y--, x-- 
        System.out.println(--x);
       
    }
}

Output: 4, 9    


මේකෙත් කලින් විස්තර කරපු operator එක වගේමයි. වෙනස තියෙන්නේ මේක decrement එක වීමයි.  Post decrement operator එක තේරුම් ගන්නනම් යට දීල තියන program එක run කරල බලන්න.

public class BasicOperatorsOne {

    public static void main(String[] args) {

for(int i =10; i > 0 ; i--)
        System.out.println(i);
       
    }
}

Output: 10,9,8,7,6,5,4,3,2,1

  
මේ image ටික බලලා for loop එකත් increment operator එකත් වැඩකරන ආකාරය පිලිබද අදහසක් ගන්න. පලවෙනියටම i අගය 0 නිසා output එක 0 වෙනවා. දෙවෙනි පාර compile වෙනකොට i අගය 1 වෙනවා. ඒක වෙන්නේ, පලවෙනිපාර 0 ලෙස output එක ආවට පස්සේ ඊලගට compile වෙන්න පටන් ගන්නේ (i++) increment operator එක ලග ඉදන්. ඒ වෙනකොට i ගේ අගය බින්දුවයි increment එක මගින් බින්දුවට එකක් එකතුවෙනව එතකොට i ගෙ අගය එකක් වෙනවා. ඊටපස්සේ අපි දීල තියන condition එක check කරනවා (i<10). එක හරිනම් ආයෙත් i ගෙ අගය output එකක් විදිහට දෙනවා. මේ රටාවටම අපි දීලා තියන condition එක වරදිනකන්ම (false) output දෙනවා. 

post decrement operator එකට example විදිහට දීලා තියන program එකෙත් වෙන්නේ මේ සිද්ධියමයි. වෙනස වෙන්නේ decrement නිසා 10 ඉදන් 1 ට print වෙනඑකයි. 













අවුලක් තියනවනම් comment කරන්න. ඊලග කොටසින් හම්බෙමු එහෙනම්… ස්තූතියි ඈ. 

class variable and instance variable in Java Class variables (Static variable) Class variables are variables declared within a class,...