aspose file tools*
The moose likes Beginning Java and the fly likes Making custom StringBuilder (or StringBuffer) - forget thread-safe / unsafe Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Making custom StringBuilder (or StringBuffer) - forget thread-safe / unsafe" Watch "Making custom StringBuilder (or StringBuffer) - forget thread-safe / unsafe" New topic
Author

Making custom StringBuilder (or StringBuffer) - forget thread-safe / unsafe

vaibhav panghal
Greenhorn

Joined: Feb 24, 2008
Posts: 25
Hi all,

I am writing here after quite some time.
I am faced with a problem - what does String class actually do to make its objects immutable or in other words, how does StringBuilder(or StringBuffer) differ from String class in that their objects are mutable ?

I mean what code in String ( or StringBuffer, for that matter ) makes it behave the way it does behave ?

For example, lets assume that we only have String class in java that makes immutable objects and we don't have StringBuilder or StringBuffer for making mutable objects and we need that functionality. So how should I make my "MutableString" class so that when I make its objects they are mutable ??

I've been pondering over this for some time now.

Thanking in anticipation.


Regards,
Vaibhav
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

String, StringBuilder and StringBuffer all use a char[] for internal storage. The main difference is, String does not have any methods that allows you to change that array, whereas StringBuilder and StringBuffer do.

Your MutableString class should simply have methods that allows you to change that array. It could look like this (very naive and simple implementation):


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
vaibhav panghal
Greenhorn

Joined: Feb 24, 2008
Posts: 25
alright. The code that you have provided is pretty straight forward and simple to understand. But it still does make use of String object which is immutable. So the purpose of having "MutableString" has been defeated..!

But thanks anyways for trying to answer my query.

Actually I was thinking on the lines of string constant pool that JVM maintains. This is the space that it looks at whenever a String object is made. If it's content is available over there then it doesn't make a new String object and simply directs the String reference to that location.

So any better answers to my original query.

Thanks in advance for trying.

Regards,
Vaibhav
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

vaibhav panghal wrote:alright. The code that you have provided is pretty straight forward and simple to understand. But it still does make use of String object which is immutable. So the purpose of having "MutableString" has been defeated..!

Then replace String with MutableString, and give your class methods for retrieving the length and any character.

In fact, let's make it a bit better:
This way your add method can accept anything: String, StringBuilder, StringBuffer, but also MutableString itself.

Of course this class is very inefficient; nearly every addition will lead to a resize of the array. StringBuilder and StringBuffer solve this by having the array size duplicate each time it becomes too small; this gives you extra space for future additions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Making custom StringBuilder (or StringBuffer) - forget thread-safe / unsafe
 
Similar Threads
why dont we prefer to use StringBuffer
Why 'equals' methods were not overridden in StringBuffer and StringBuilder?
Creating our own String class?
Why String class is immutable
Difference between Object's equals() and String's equals() method ?