ජාවා 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,...