Java dasturlash tili. - Har bir obektga mo’ljallangan dastur- lash tili quyidagi xarakterlarga ega bo’lishi kerak.
- 1) Sinf tushunchasi (meroslash)
- 2) Inkapsulyatsiya
- 3) Polimorfizm
- Dastur ichki qism va interfeys qismdan iborat
Barcha dasturlash tillari kabi Java dasturlash tili ham o’zining - Sintaksiz
- Semantik
- Progmatika siga ega
Izoh - /* izoh*/ - blokli izoh
- // izoh – satrli izoh
- Bir necha satrli izoh
- /*
- izoh1-satr
- izoh2-satr
- …
- izohn-satr
- */
- Bir necha satrli izoh (hujjat)
- /**
- izoh1-satr
- izoh2-satr
- …
- izohn-satr
- */
Sonli tiplar (Butun) | | | | | | | | | | | - -32768 dan tо
- 32767 gacha
| | | | - -2147483648 dan to
- 2147483647 gacha
| | | | - -9223372036854775808L dan to
- 9223372036854775807L gacha
| Sonli tiplar (Haqiqiy) | | | | | | | - taxminan
- ±3.40282347E+38F
- INFINITY
- -INFINITY
- NaN
| | | | - taxminan
- ± 1.7976931348623157E+308D
- INFINITY
- -INFINITY
- NaN
| Sonli literallar | | | | | | | | - –30, 489, 15000, 010 (=8)
| | | | | | | | | - –3.45F, 1.0F, 567.9876F, 1e+5F
| | | - –3.45, 1.0, 567,98763.14, 2., .5, 7e10
- 3.1E-20
| Simvol tipi(char) - Xotira 2 bayt
- Qiymatlar sohasi '\u0000' dan 'uFFFF gacha
- Literallar misol: ‘h’, ‘\n’, ‘\u002A’
- '\u0041' // lotincha A harfi
- '\u0410' // ruscha А harfi
- '\u0391' // grekcha A harfi
- Maxsus literallar:
- literal ma’nosi unicode
- \b 1 positsiyaga orqaga qaytish \u0008
- \t tabulyatsiya \u0009
- \n yangi satrga o’tish \u000a
- \“ qo’shtirnoq \u0022
- \’ tirnoq \u0027
- \\ teskari slesh \u005c
Mantiqiy tip(boolean) - Qiymatlar sohasi:
- true
- false
Satr tipi (String) - Simvollar massivi
- “” uzunligi 0 satr
- “\”” 1 ta simvoldan iborat satr
- “Oddiy satr” uzunligi 10 satr
- “Salom dunyo!\nDunyo!” 2-qatordan iborat satr
O’zgaruvchilar - E’lon:
- [’ ‘]’ ‘{‘,’}
- Misol:
- double maosh;
- Int ishkuni;
- long a;
- char x;
- boolean jinsi;
- Qiymat berish va initsializatsiya:
- int ishkuni; // bu — e’lon
- ishkuni = 12; // bu — qiymat berish operatori
- int ishkuni = 12; // bu — initsializatsiya
- String ism = “Alisher” // massivga ko’rsatgich
- String s = “Alisher” // o’sha massivga ko’rsatgich
Konstantalar - [’ ‘] final
- Misol:
- final double K = 2.54;
- Sinf konstantasi:
- static final double Y = 2.54;
- Global konstanta:
- public static final double R = 2.54;
Misol: fayl nomi oddiy.java - class oddiy{
- public static void main(String a[ ]){
- int b=4;
- double x,y;
- String s1;
- x=110;
- y=x+b;
- s1="Natija=";
- System.out.println(s1+y); } }
Global o’zgaruvchiga misol: fayl nomi global.java - class s{
- public static final int n=2;}
- class global{
- public static void main(String a[]){
- int b=4;
- System.out.println(s.n); } }
Operatorlar(Arifmetik) - +, -, *, /, %, +=, -=, *=, /=, %= (binar); -, +, ++, -- unar
- Misollar:
- 12/0, 0/0 – favqulotda hodisa (/ by zero)
- 12.0/0 – Musbat cheksiz son(Float.POSITIVE_INFINITY)
- -12.0/0 – Manfiy cheksiz son(Float.NEGATIVE_INFINITY)
- int i1=2147483647;
- Int i2=1;
- int i3=i1+i2; // -2147483648; aniqlini yo’qotish bo’lmaydi (loss of precision)
- short i3 = i1; // kompilyatsiya: aniqlik yo’qotiladi (loss of precision)
- short i4 = i2; // kompilyatsiya: aniqlik yo’qotiladi (loss of precision)
- short i4 = (short)i2; //1
- х += 4; ekvivalent x=x+4; (qisqrtma)
- int m = 7;
- int n = 7;
- int a = 2 * ++m; // a=16, m=8
- int b = 2 * n++; // a=14, n=8
Operatorlar(Munosabat) - ==, !=, <, <=, >, >= (binar)
- ==, != hamma ob’ektlar uchun
- <, <=, >, >= sonli tiplar va char uchun
- int ii=1;
- short is=1;
- float f=12.0F;
- double d=f;
- String s1="maktab";
- String s2="mak"+"tab";
- System.out.println(ii==is); //true
- System.out.println(f==d); //true
- System.out.println(s1==s2); //true
Operatorlar(Mantiqiy) - ?: (ternar) |, &, ^, ||, &&, (binar)
- ! (unar)
- Ternar operator:
- ?:
- Misollar:
- x = x > 0 ? x : -x; // |x|
- int x = 9;
- int y = 55;
- x < 10 && y > 20; // true
- x == 9 && y == 100; // false
- x < 10 || y > 20; // true
- x == 9 || y == 100; // true
- x < 10 & y > 20; // true
- h = x == 10 & y == 55; // false
- x < 10 | y > 20; // true
- x == 10 | y == 55; // true
- x < 10 ^ y > 20; // false
- x == 9 ^ y == 100; // true
- x == 10 ^ y == 55; // true
- !(x == y); // true
- !(x < 10 && y == 100); // true
- !(y == 55); // false
Operatorlar(Bitli) - &, |, ^ (binar); ~ (unar)
- 5&6 // 4
- 00000101
- &
- 00000110
- -------------
- 00000100
- ~5 // -6
- ~00000101
- ---------------
- 11111010
- int fourthBitFromRight = (n & 8) / 8; // =1
Operatorlar(Siljish) - <<, >>, >>> (binar)
- 20 << 00 = 00000000000000000000000000010100 = 20
- 20 << 01 = 00000000000000000000000000101000 = 40
- …
- 20 << 29 = 10000000000000000000000000000000 = -2147483648
- 20 << 30 = 00000000000000000000000000000000 = 0
- 20 << 31 = 00000000000000000000000000000000 = 0
- -21 << 00 = 11111111111111111111111111101011 = -21
- -21 << 01 = 11111111111111111111111111010110 = -42
- ...
- -21 << 29 = 01100000000000000000000000000000 = 1610612736
- -21 << 30 = 11000000000000000000000000000000 = -1073741824
- -21 << 31 = 10000000000000000000000000000000 = -2147483648
- -21 >>> 00 = 11111111111111111111111111101011 = -21
- -21 >>> 01 = 01111111111111111111111111110101 = 2147483637
- ...
- -21 >>> 29 = 00000000000000000000000000000111 = 7
- -21 >>> 30 = 00000000000000000000000000000011 = 3
- -21 >>> 31 = 00000000000000000000000000000001 = 1
Tiplarni almashtirish(kengaytirish) Binar operatsiya tipi - Agar kamida 1 ta operand double tipida bo’lsa 2-operand ham double tipiga almashtiriladi.
- Aks holda agar kamida 1 ta operand float tipida bo’lsa 2- operand ham float tipiga almashtiriladi.
- Aks holda agar kamida 1- ta operand long tipida bo’lsa 2- operan ham long tipiga almashtiriladi.
- Aks holda 2-la operand ham int tipiga almashtiriladi.
Tiplarni keltirish(toraytirish-cast) - byte → char
- short → byte; short → char;
- char → byte; char → short;
- int → byte; int → short; int → char;
- long → byte; long → short; long → char; long → int;
- float → byte; float → short; float → char; float → int; float → long;
- double → byte; double → short; double → char; double → int;
- double → long; double → float;
- double х = 9.997;
- int nx = (int)x; //nx =9
- double x = 9.997;
- int nx = (int)Math.round(x); //nx = 10
Operatorlar iyerarxiyasi - [] . () (metodni chqirish) chapdan o’ngga
- ! ~ ++ -- +(unar) –(unar) ()(keltirish) o’ngdan chapga
- * / % chapdan o’ngga
- + - chapdan o’ngga
- << >> >>> chapdan o’ngga
- == != chapdan o’ngga
- & chapdan o’ngga
- ^ chapdan o’ngga
- | chapdan o’ngga
- && chapdan o’ngga
- || chapdan o’ngga
- ?: chapdan o’ngga
- = += -= *= /= %= |= ||= <<= >>= >>>= o’ngdan chapga
String tipi - String e = ""; // Bo’sh satr
- String greeting = "Hello";
- String expletive = “Oddiy";
- String PG13 = “ satr";
- String message = expletive + PG13;
- int age = 13;
- String rating = "PG" + age; //rating=PG13
- String s1 = "abc";
- String s2 = "abc";
- String s3 = "a"+"bc";
- String s4="ab";
- System.out.println(s1==s2); //true
- System.out.println(s1==s3); //true
- System.out.println(s1==(s2+"c")); //false
Ko’rsatgich tiplar - char theCharacter = ‘J’;
- String title = “Java”;
- String, Massiv va Sinflar
- ko’rsatgich tiplari hisoblanadi
Bajarilishni boshqarish operatorlari(tarmoqlash) - If () ;
- --------------------------------
- If () ; else ;
- ---------------------------------
- If () ;
- else if () ;
- …
- else if () ;
- [else ;]
- ----------------------------------
- Blok operatori
- {; ; …;}
Bajarilishni boshqarish operatorlari(tanlash) - switch() {
- case : [operator1; break;]
- case : [operator2; break;]
- …
- case : [operator3; break]
- [; default: ]
- };
- - faqat byte, short, int, char tiplarida bo’lishi mumkin
- switch(c) {
- case ‘a’:
- case ‘A’:
- case ‘o’:
- case ‘O’:
- case ‘u’:
- case ‘U’: System.out.println(c + “ is a vowel.”); break;
- case ‘y’:
- case ‘Y’:
- case ‘w’:
- case ‘W’ System.out.println(c + “ is sometimes a vowel.”); break;
- default: System.out.println(c + “ is not avowel.”);
- };
Bajarilishni boshqarish operatorlari(tsikllar) - while ()
- Misol:
- while (i<=n) i++;
- while (i>100) {s=i; k=s+p; i--}
- for (; ; ) operator
- Misol:
- for (int i=1; i<=n; i++) s+=I;
- For (k=10; k>0; k--) {s+=k; System.out.println(s);}
- do while ()
- Misol:
- do i--; while (i>10);
- do {x+=4; System.out.println(x)} while(x<100);
|