I read about mutable / immutable objects. Immutable objects have many useful properties. But it is possible to distinguish two that are characteristic of practically all immutable objects:

1) Immutable objects can be implemented much easier than mutable ones. 2) Immutable objects can be freely used simultaneously from different threads.

I have a few questions, please ask if you answer and it’s not difficult for you to answer me point by point, thanks in advance :)

1) How to understand objects can be implemented much easier?

2) Correct information or not? - An immutable object is an object whose external visible state cannot change after its creation. The String, Integer, and BigDecimal classes in the Java class library are examples of immutable objects — they represent a separate value that cannot change during the object's life cycle.

3) What are mutable / immutable objects besides classes String (StringBuilder, StringBuffer), Integer, BigDecimal? Just the names I want to see.

4) Immutable objects can be freely used simultaneously from different threads. Threads it flows? Are they synonymous?

Closed due to the fact that the issue is too general for the participants Sergey Gornostaev , VTT , 0xdb , Kirill Korushkin , aleksandr barakin 10 May at 14:53 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

    1 answer 1

    Much simpler, because when an object cannot change in principle:

    • There is no need for additional checks, synchronization of the record, maintenance of the class invariant after changing fields.
    • There is no need to copy shared data.

    Besides

    • It is possible to cache instances, both explicitly and at the discretion of the compiler.
    • More aggressive optimization becomes possible.

    The list of immutable classes in the standard library is constantly updated, but more or less a complete list was attempted to be given in this answer .

    Free use is possible and impossible, because an immutable object may well contain variable fields, but in the general case it is much safer to use immutable objects in multi-threaded applications, and in some cases absolutely safe.

    • Why are shell classes written and not primitive types? After all, these are different things, I just noticed that the primitive type int is also immutable - Ivan Petrovchenko
    • @PetrovchenkoIvan Classes are written, not necessarily wrappers. int is not a class at all. In general, the list was simply to be, because in the external libraries of such classes is not considered, and they are used everywhere in java, and in other languages, perhaps even more often than mutable. - extrn
    • I know that int is not a class, but int also refers to immutable objects? I just made an example and it turned out like with a String, so I ask - Petrovchenko Ivan
    • @PetrovchenkoIvan These types are simply not classified in terms of mutable / immutable , just as the number 1 is not simple and not composite. int not passed by reference, only by value, so on the one hand a variable of type int can change the content, in this sense it is mutable , but if we took int for example from an array, and then modified the value in the array, then we will not change it. in this sense, it is immutable . - extrn
    • You do not know how to go to chat discussions? I just want to give you a small example, I have one question there - Ivan Petrovchenko