Dasturlashda Mutable, Immutable obyektlar

fazliddinxamdamov

Fazliddin Xamdamov

Posted on March 17, 2022

Dasturlashda Mutable, Immutable obyektlar

Mutable obyektlar yaratilganidan keyin uni o'zgartirish mumkin, immutable obyektlarda esa aksincha o'zgartirib bo'lmaydi.

Javada (Strindan tashqari) hamma obyektlar default holatda mutable hisoblanadi.

 public class IntegerPair {
    int x;
    int y;

     IntegerPair(int x, int y) {
        this.x = x;
        this.y = y;
     }
 }

 IntegerPair p = new IntegerPair(5, 10);
 // p.x = 5, p.y = 10

 p.x = 50;
 // p.x = 50, p.y = 10
Enter fullscreen mode Exit fullscreen mode

Mavjud ob'ektlarni o'zgarmas qilishning hech qanday usuli yo'q. Agar ob'ekt yakuniy deb e'lon qilingan bo'lsa ham, uning field(atribut)larini o'zgartirish mumkin:

public class IntegerPair {
    int x;
    int y;

    IntegerPair(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

final IntegerPair p = new IntegerPair(5, 10);
// p.x = 5, p.y = 10

p.x = 50;
// p.x = 50, p.y = 10
Enter fullscreen mode Exit fullscreen mode

Ya'ni, agar siz o'z classingizmi chaqirib, barcha fieldlarini yakuniy(final) va shaxsiy(private) qilish orqali uning ob'ektlarini o'zgarmas qilishingiz mumkin.

public class IntegerPair {
    private final int x;
    private final int y;

    IntegerPair(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

IntegerPair p = new IntegerPair(5, 10);

p.x = 50;
// Compilation error: cannot assign a value to a final variable
// kompiler xatosi final o'zgaruvchinga qiymat berolmaydi 
Enter fullscreen mode Exit fullscreen mode


String tilga qarab oʻzgaruvchan(mutable) yoki oʻzgarmas(immutable) boʻlishi mumkin.

Java-da satrlar o'zgarmasdir.


Har qanday stringni o'zgartirganingizda (masalan: qo'shimcha belgini qo'shganizda, uni kichik harflarga aylantirganizda, ikkita belgini almashtirganizda), siz aslida yangi va alohida nusxani yaratgan bo'lasi,ishoning :)

  String first = "first";

System.out.println(first.hashCode());
// prints something

first = first + "!";

System.out.println(first.hashCode());
// different string, different hash code
Enter fullscreen mode Exit fullscreen mode

Ammo ba'zi boshqa tillarda, masalan, C++, stringlar o'zgaruvchan bo'lishi mumkin va biz ularni to'g'ridan-to'g'ri o'zgartirishimiz mumkin:

  string testString("mutable?");

testString[7] = '!';
// testString is now "mutable!"
Enter fullscreen mode Exit fullscreen mode


Agar siz mutable ya'ni o'zgartirsa bo'ladigan stringdan foydalanmoqchi bo'lsangiz , sizga StringBuilder/StringBuffer yordamga keladi.StringBuilder dan foydalanganingiz maqul chunki u buffer-ga qaraganda samaraliroq.

  StringBuilder mutableString = new StringBuilder("mutable?");

mutableString.setCharAt(7, '!');
// still the same object!
// mutableString is now "mutable!"

// convert to an immutable string
String immutableString = mutableString.toString();
Enter fullscreen mode Exit fullscreen mode

Ammo siz stringni o'zgaruvchan char-lar qatoriga aylantirishingiz ham mumkin.

O'zgaruvchan ob'ektlar(mutable) yaxshi, chunki siz yangi ob'ektni ajratmasdan o'z joyida o'zgartirishlar kiritishingiz mumkin. Ammo ehtiyot bo'ling - ob'ektga o'z joyida o'zgartirish kiritganingizda, ushbu ob'ektga qilingan barcha havolalar endi o'zgarishlarga uchraydi.

💖 💪 🙅 🚩
fazliddinxamdamov
Fazliddin Xamdamov

Posted on March 17, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related