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);

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

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