Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to create user defined immutable class

 
kumarjit banerjee
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you're saying about strings is false. Consider this example:


The point is that comparison using == compares the actual object references. If you want to check for equality you should override hashCode() and equals();
 
kumarjit banerjee
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:What you're saying about strings is false. Consider this example:


The point is that comparison using == compares the actual object references. If you want to check for equality you should override hashCode() and equals();



Hi,

I understand that creating Strings one with literal and another with the constructor provide two different objects. But if they they are created both as literals, then only one String object is created and two references are there. I want to implement a class which will not create another object unless the state of the object is not there in the heap. If an object with the same state in the heap exists then that object will be returned. While creating instances with the new operator each time a new object is created. Now coming to the point of overrriding equals() and hashCode(), these methods are used to compare whether two objects are meaning fully equal, but they still refer to two different positions in the memory. I want to know is there any way to check whether a meaning fully equal object is present in the heap, then without creating a new object the reference to the same object will be returned, which will pass the == condition?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then enhance your code by adding a cache (using a Map or something like that). But remember because you're caching all the instances they will not be garbage collected. So if you create a huge number of instances your memory usage will be very high. A solution to that could be using WeakReference but that is certainly not "Beginning Java".
 
Campbell Ritchie
Sheriff
Pie
Posts: 50258
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote: . . . but that is certainly not "Beginning Java".
Nor is the rest of this discussion Moving.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic