What’s the difference of an object being final and an object being immutable in java?

final String str = "do not change me";
str = "why not?"; //it will result in compile time error saying that final fields can not be re-assigned once created i.e. the reference cannot be changed

String str1 = "hello world";  
    System.out.println(str1.replaceAll("h", "q"));
    str1 = str1.replaceAll("h", "q");
    System.out.println(str1);  //An immutable object on the other hand means that, there is no way for you to change the object or its contents other than changing the reference.

Is the above understanding of definition of a final object and immutable object correct?

2

In Java, objects are not final. Instead: variables, classes, or methods are. A final variable cannot be reassigned, so the variable becomes interchangeable with the value it represents. This is roughly known as “referential transparency”.

An immutable object is an object without changing state. Simplified: If all member fields of an object are final variables, then it’s quite likely that the object is immutable as well. If we need an object with changed state, then we have to return a new instance. This is what str.replace(…) does, because Strings are immutable in Java.

Objects can be immutable and references to them can be final; but the reverse is mostly technical confusion. A “final object” is really a final reference to an object, while an “immutable reference” (or “immutable field” or “immutable variable”) is rarely mentioned because it is not how they are considered. A reference is effectively an address, and therefore like a primitive number or boolean. A variable or field can contain the integer 10. If you increment that integer, are you “mutating” that 10, or are you really assigning a different integer? On the bit level, you are changing the contents of a single memory location, but at the semantic language level it is the latter, since everywhere else that uses 10 is unchanged.

A final field or variable must be assigned exactly once, and the compiler will enforce this. An immutable object does not change once it has completed construction, but there is no real enforcement. Note that even if you define an object that has only final fields, that does not automatically make the object immutable. If a final field is an object reference, that nested object must be immutable. There is also no way at the language level to make an array immutable; a final reference to an array means you cannot change which array or its length, but you can change its contents. The language cannot enforce making such an object immutable. So in many cases, an object is immutable only because you intend it to be (and if there’s a bug in your code, maybe it isn’t).

String objects are like that: they have a final array of characters, and are immutable only because the methods that might change something return other instances instead. Methods like substring can actually share the original character array and simply “see” a different section of that array.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật