It's not a secret anymore!*
The moose likes Java in General and the fly likes How to make String Class Mutable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to make String Class Mutable" Watch "How to make String Class Mutable" New topic
Author

How to make String Class Mutable

Manjusha Muraleedas
Ranch Hand

Joined: Jun 02, 2010
Posts: 52

Hi All,

We all know that String class is Immutable.

I recently got a Question in an interview that...How to make a String class mutable?

Is it possible to make a String class Mutable???




pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
I'd use StringBuilder if I needed a class that holds a mutable sequence of characters and handles Strings well.
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

I don't think it's possible.


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Freddy Wong wrote:I don't think it's possible.


I believe that it is possible through reflection, but not good practice for you break String's contract and risk a whole host of bugs.

Edit: Here's proof: http://www.cs.princeton.edu/algs4/12abstraction/MutableString.java
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

Alright, I stand corrected I guess with reflection and bytecode manipulation, it's possible to do pretty much everything :p
Manjusha Muraleedas
Ranch Hand

Joined: Jun 02, 2010
Posts: 52
Thank you so much pete for the excellent answer......
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Please note that the code in Pete's link is purely academical. Never ever use this in real life code.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Rob Prime wrote:Never ever use this in real life code.

thanks for the information
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11250
    
  16

If someone asked me "How would you make the String class mutable" my answer would be "I wouldn't, and I'd fire anyone who did".


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Manjusha Muraleedas
Ranch Hand

Joined: Jun 02, 2010
Posts: 52
This was the exact Question for my interview. But I answered ... "StringBuffer can be used".

That was a wrong answer.StringBuffer is the alternate solution.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Manjusha Muraleedas wrote:StringBuffer is the alternate solution.


StringBuilder, even better
Manjusha Muraleedas
Ranch Hand

Joined: Jun 02, 2010
Posts: 52
Seetharaman Venkatasamy wrote:

StringBuilder, even better



I think StringBuffer is the best option because strings are thread safe

StringBuffer is thread safe but StringBuilder is not
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

While we're starting to get off topic, the choice of StringBuffer or StringBuilder would be decided by their usage. I tend to keep String building local to a method and not share the builder, so I tend to use StringBuilder over StringBuffer, but it would be wrong to use one or the other without considering why.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

David O'Meara wrote: I tend to keep String building local to a method and not share the builder, so I tend to use StringBuilder over StringBuffer, but it would be wrong to use one or the other without considering why.

short-off . good point
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Manjusha Muraleedas wrote:I think StringBuffer is the best option because strings are thread safe

StringBuffer is thread safe but StringBuilder is not

The documentation of StringBuffer says:
As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.

In practice, you almost never use the same StringBuffer object from multiple threads at the same time. Synchronization adds overhead, and most of the time it's not necessary. So you'd better use StringBuilder, unless you have a very specific reason to use StringBuffer.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

fred rosenberger wrote:If someone asked me "How would you make the String class mutable" my answer would be "I wouldn't, and I'd fire anyone who did".


this is the correct answer


http://plainoldjavaobject.blogspot.in
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18753
    
  40

pete stein wrote:
I believe that it is possible through reflection, but not good practice for you break String's contract and risk a whole host of bugs.

Edit: Here's proof: http://www.cs.princeton.edu/algs4/12abstraction/MutableString.java



It's actually an interesting solution, but it may not work with all JVMs (or if certain products are used). Some third party JVMs have there own implementation of String -- and it's implemented differently. And the private fields that is being changed, may be different, or may not even exist.

I ran into this issue a few years ago, when mixing two products, with a non-Sun JVM. Ran into a whole bunch of errors related to the String class.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Manjusha Muraleedas
Ranch Hand

Joined: Jun 02, 2010
Posts: 52
Thanks Hentry .

So the implemetation of Built in classes varies depending on the JVMs?

I am confused....whats is the validity of the sun's documentations then..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Are the private fields of String part of this documentation? I think not. And that's why Sun is allowed to change them completely, as long as the public and protected members do not change. Anything not documented (i.e. not in the API) should be considered as a black box and no assumptions should be made about them. That includes Sun's classes for internal use, in packages starting with sun, com.sun etc.
Manjusha Muraleedas
Ranch Hand

Joined: Jun 02, 2010
Posts: 52
Thank you so much for this information....
Gerard Charles
Greenhorn

Joined: Jan 21, 2010
Posts: 11
Wrap String in apache commons MutableObject
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

Wow, whoever thought of that class must have had a really bad day. Why would you possibly want a MutableObject?
Gerard Charles
Greenhorn

Joined: Jan 21, 2010
Posts: 11
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Gerard Charles wrote:


No thanks.

If I were going to go that way, I'd just use StringBuilder. Or possibly java.util.concurrent.atomic.AtomicReference<String>. But I wouldn't go that way anyway, so it's a moot point.

But I wouldn't do it that way. I'd either simply return the explanation, with null indicating no problem; or if I wasn't feeling like using one thing (the return value) for two purposes (good/bad result + message), I might return void, and throw an exception if it failed (a common pattern in validating required conditions); or I'd do this:



Regardless, I am of the opinion that Mutable object is generally icky and leaves me wanting a shower.

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Jeff Verdegan wrote:
Regardless, I am of the opinion that Mutable object is generally icky and leaves me wanting a shower.

I'm not so much revolted as puzzled by it. What situation could arise which would make it useful?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Manjusha Muraleedas wrote:This was the exact Question for my interview. But I answered ... "StringBuffer can be used".

Was the question...: "How to make the java.lang.String class mutable?"

or was it: "How to make a mutable string class?" (as in: you're designing your own alternative class for strings)

Because those questions are quite different.
Ankit Sanghavi
Greenhorn

Joined: Jun 15, 2012
Posts: 2

so what is the last answer for question, Is it possible String mutable class.............?
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Welcome to the Ranch, Ankit!

I'd say this discussions actually answers the question pretty fine. I'd suggest reading it thoroughly.
Ankit Sanghavi
Greenhorn

Joined: Jun 15, 2012
Posts: 2

thank you for welcoming me, I have read all post... but not got the answer...!!!
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

Ankit Sanghavi wrote:thank you for welcoming me, I have read all post... but not got the answer...!!!

The correct answer is there if you look for it. Fred Rosenberger said it the best. I could say it a different way but you wouldn't want that. (<-- closest thing to "The Hulk" that we have around here)

Who the heck comes up with interview questions like this anyway? IMO, questions like this and this are very poor ways of finding good people to hire. It's like interviewing drivers who will eventually drive your kids around and you asking them "Are you able to get a car to go up to 115 mph, then do a 360-degree spinout?" Do you really want to hire the guy who says "Yes" to this? I think they should fire the interviewer.


Junilu - [How to Ask Questions] [How to Answer Questions]
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Ankit Sanghavi wrote:thank you for welcoming me, I have read all post... but not got the answer...!!!

Read Henry's post including the post he quoted and you will have your answer.

But take careful note of Rob's post.
Rob Spoor wrote:Please note that the code in Pete's link is purely academical. Never ever use this in real life code.
dirk dj jaeckel
Greenhorn

Joined: May 31, 2012
Posts: 22


4 0 4


http://algs4.cs.princeton.edu/12abstraction/MutableString.java


Apache/2.2.21 server at algs4.cs.princeton.edu


Last modified on December 17, 2010.

Copyright © 2002–2012 Robert Sedgewick and Kevin Wayne. All rights reserved.
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

I think your interviewer had not worked on java since a long time and did not have real questions to ask.
My personal opinion after a considerable interview experience( on both ends of the table ) - This is a typical question "Not so good developers" ask others in interviews.

Anrd
"One of the best things you could do is to simplify a larger application into a smaller one by reducing its process and complexity - Fowler"
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

fred rosenberger wrote:If someone asked me "How would you make the String class mutable" my answer would be "I wouldn't, and I'd fire anyone who did".

So true. Again, some lazy guy interviewed you. My suggestion - don't join that firm
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
dirk dj jaeckel wrote:


4 0 4


http://algs4.cs.princeton.edu/12abstraction/MutableString.java


Apache/2.2.21 server at algs4.cs.princeton.edu


Last modified on December 17, 2010.

Copyright © 2002–2012 Robert Sedgewick and Kevin Wayne. All rights reserved.

Did you try putting MutableString into the search box on that page ?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4369
    
    8


Try http://algs4.cs.princeton.edu/12oop/MutableString.java.html, it looks like it's the same one.
 
Don't get me started about those stupid light bulbs.
 
subject: How to make String Class Mutable