aspose file tools*
The moose likes Java in General and the fly likes How to create user defined immutable class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to create user defined immutable class" Watch "How to create user defined immutable class" New topic
Author

How to create user defined immutable class

kumarjit banerjee
Ranch Hand

Joined: Mar 27, 2011
Posts: 32

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

Joined: Oct 25, 2008
Posts: 2700

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();


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
kumarjit banerjee
Ranch Hand

Joined: Mar 27, 2011
Posts: 32

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

Joined: Oct 25, 2008
Posts: 2700

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

Joined: Oct 13, 2005
Posts: 38340
    
  23
Wouter Oet wrote: . . . but that is certainly not "Beginning Java".
Nor is the rest of this discussion Moving.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to create user defined immutable class