What are an Immutable Objects in Java?
Hi friends,
I have recently started with the threads of Java programming. Before doing Java, I did C++ and HTML programming language. But I never heard about the immutable objects. Hoping that guys over there would help me on this topic. Please tell me what are an Immutable Objects in Java? And also please tell me how to define an Immutable Objects.!! Help me as soon as possible.!!:notworthy
Re: What are an Immutable Objects in Java?
The following rules define a simple strategy for creating immutable objects. Not all classes documented as "immutable" follow these rules. An immutable object is an object whose state cannot be modified after it is created. An object can be either entirely immutable or some attributes in the object may be declared immutable. Such strategies require sophisticated analysis and are not for beginners. It will be better for you to not have a look on this rules. Because you will not understand anything of that.
Re: What are an Immutable Objects in Java?
In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created. Since you have done C++, I would like to add that its similar using the const member data attribute in the C++ programming language. In some cases, an object is considered immutable even if some internally used attributes change but the object's state appears to be unchanging from an external point of view. An object is considered immutable if its state cannot change after it is constructed.
Re: What are an Immutable Objects in Java?
Maximum reliance on immutable objects is widely accepted as a sound strategy for creating simple, reliable code. Immutable objects are particularly useful in concurrent applications. Since they cannot change state, they cannot be corrupted by thread interference or observed in an inconsistent state. All Java variables are by default mutable. You can make them immutable by using the final keyword. You can also make them private and provide no accessors to the outside world to change them. Programmers are often reluctant to employ immutable objects, because they worry about the cost of creating a new object as opposed to updating an object in place.
Re: What are an Immutable Objects in Java?
Mutable objects are objects whose state can change. Immutable objects are objects whose state never changes after creation. Immutable objects have many desirable properties. You can share immutable objects between threads without danger of changes confusing the other thread. Immutable objects are thread-safe. Threads cannot corrupt what they cannot change. Immutable objects make good hash keys, since their hash codes cannot change. Two threads can both work on an immutable object at the same time without any possibility of conflict.