I have learned from the internet that if a class is declared as follows for example is immutable.
The objects of this class are immutable. Every time an object is created is a new instance of the class Immutable.
But I want to know whether there is any technique to define immutable classes which will have similar behavior with respect to immutability with that of the
String class.
For example,
When we write String s1="abc" and String s2="abc", then only one String "abc" is created and s1==s2 is true, but when we try to modify the string value a new object is created as s2=s2+"d", now s1="abc" but s2="abcd". So until and unless the state is modified the reference variables s1 and s2 refers to the same object.
I want to say that when we create two instances of Immutable class if their values are equal then they will be the same object For example,
Immutable im1 = new Immutable("abc",1);
Immutable im2 = new Immutable("abc",1);
then im1==im2 is false.
I am aware that if we create the String objects in this way they will fail the == condition,
as String s1=new String("abc");
String s2=new String("abc");
then s1==s2 is false.
So I want to know if I can implement like this
Hope I have conveyed correctly what I want to know.