1. Unar(birlik) operatorlari
2. Arifmetik opertorlari
3. Shart operatorlari
4. Mantiqiy operatorlar
5. O'zlashtirish operatorlari
6. Va boshqa operatorlar
Arifmetik operatorlari
Operator | Natija |
+ | Qo'shish |
- | Ayirish |
* | Ko'paytirish |
/ | Bo'lish |
% | Qoldiq olish |
++ | Increment(bittaga oshirish) |
+= | Qo'shish |
-= | Ayirish |
*= | Ko'paytirish |
/= | Bo'lish |
%= | Qoldiq olish |
-- | Decrement(Bittaga kamaytirish) |
Misollar:
public class MainClass {
public static void main(String args[]) {
int aab = 1;
int bbb = 2;
System.out.println("Sonlar ustida arifmetik amallar");
int a = 1 + 1; //qo'shish
int b = a * 3; //ko'paytirish
int c = b / 4; //bo'lish
int d = c - a; //ayirish
int e = -d; //unar (-) operatori
int k = 11 % 3; //qoldiq olish
aaa += 5; //aaa = aaa + 5
bbb *= 4; //bbb = bbb * 4
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
System.out.println("e = " + e);
System.out.println("k = " + k);
System.out.println("aaa = " + aaa);
System.out.println("bbb = " + bbb);
}
}
Natija:
Sonlar ustida arifmetik amallar a = 2 b = 6 c = 1 d = -1 e = 1 k = 2 aaa = 6 bbb = 8
Increment va Decrement ga misollar:
public class MainClass {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c;
int d;
c = ++b; //b = 3 , c = 3 b ning qiymati 1 ga oshirildi
d = a++; //d = 1 , a = 2 a ning qiymati 1 ga oshirildi
c++;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}
a = 2 b = 3 c = 4 d = 1
c = ++b bu yerda nima ish bajarilayotganini ko'rib chiqaylik.
Bu yerda c ga b ni qiymatini bittaga oshirib keyin o'zlashtiryapti.
d = a++ bu yerda d ga a ni qiymatini o'zlashtirdi va a ni qiymatini bittaga oshirdi
++a va a++ bu ikkalsini adashtirmang!Taqqoslash va Shart operatorlari
Taqqoslash operatorlari | Nomlanishi |
> | katta |
>= | katta yoki teng |
== | teng |
!= | teng emas |
<= | kichik yoki teng |
< | kichik |
Misollar:
import java.util.Random;
public class MainClass {
public static void main(String[] args) {
Random rand = new Random();
int i = rand.nextInt(100);
int j = rand.nextInt(100);
System.out.println("i = " + i);
System.out.println("j = " + j);
System.out.println("i > j is " + (i > j));
System.out.println("i < j is " + (i < j));
System.out.println("i >= j is " + (i >= j));
System.out.println("i <= j is " + (i <= j));
System.out.println("i == j is " + (i == j));
System.out.println("i != j is " + (i != j));
System.out.println("(i < 10) && (j < 10) is " + ((i < 10) && (j < 10)));
System.out.println("(i < 10) || (j < 10) is " + ((i < 10) || (j < 10)));
}
}
Natija:
i = 92 j = 22 i > j is true i < j is false i >= j is true i <= j is false i == j is false i != j is true (i < 10) && (j < 10) is false (i < 10) || (j < 10) is false b = 3
Mantiqiy operatorlar
Operator | Natija |
& | AND |
| | OR |
^ | XOR (exclusive OR) |
|| | OR |
&& | AND |
! | NOT |
&= | AND assignment |
|= | OR assignment |
^= | XOR assignment |
== | Tenglik sharti |
!= | Teng emaslik sharti |
?: | shart operatori |
Mantiqiy operatorlar ustida amallar
a | b | a&b | a|b | a^b | ~a yoki !a |
---|---|---|---|---|---|
true(1) | true(1) | true | true | false | false |
true(1) | false(0) | false | true | true | false |
false(0) | true(1) | false | true | true | true |
false(0) | false(0) | false | false | false | true |
Mantiqiy operatorlarga misol:
public class MainClass {
public static void main(String args[]) {
boolean a = true;
boolean b = false;
boolean c = a | b;
boolean d = a & b;
boolean e = a ^ b;
boolean f = (!a & b) | (a & !b);
boolean g = !a;
System.out.println(" a = " + a);
System.out.println(" b = " + b);
System.out.println(" a|b = " + c);
System.out.println(" a&b = " + d);
System.out.println(" a^b = " + e);
System.out.println("!a&b|a&!b = " + f);
System.out.println(" !a = " + g);
}
}
Natija:
a = true b = false a|b = true a&b = false a^b = true !a&b|a&!b = true !a = false
" ? " shart operatori
public class MainClass {
public static void main(String args[]) {
int i, k;
i = 10;
k = i < 0 ? -i : i; //i noldan kichik bo'lsa k ning qiymati -i ga,aks holda i ga teng
System.out.print("k ning qiymati "+k);
i = -10;
k = i < 0 ? -i : i;
//i noldan kichik bo'lsa k ning qiymati -i ga,aks holda i ga teng
System.out.print(
"k ning qiymati "+k
);
}
}
Natija:
k ning qiymati 10 k ning qiymati -10
Sardor Dushamov. email:dsq2190@gmail.com
Maqolani yozishda quyidagilardan foydalanildi:
http://java2s.com //sevimli saytim
http://google.com
No comments:
Post a Comment