ජාවා 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 එකක් විදිහටත් හදුන්වනවා. අපි ඒ ගෙන වෙනම කතාකරමු.
එහෙනම්
ස්තූතියි ඈ…