Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to make String Class Mutable

 
Manjusha Muraleedas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd use StringBuilder if I needed a class that holds a mutable sequence of characters and handles Strings well.
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it's possible.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, I stand corrected I guess with reflection and bytecode manipulation, it's possible to do pretty much everything :p
 
Manjusha Muraleedas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much pete for the excellent answer......
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note that the code in Pete's link is purely academical. Never ever use this in real life code.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Never ever use this in real life code.

thanks for the information
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Manjusha Muraleedas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manjusha Muraleedas wrote:StringBuffer is the alternate solution.


StringBuilder, even better
 
Manjusha Muraleedas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15274
38
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Manjusha Muraleedas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for this information....
 
Gerard Charles
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wrap String in apache commons MutableObject
 
Stephan van Hulst
Bartender
Pie
Posts: 5796
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, whoever thought of that class must have had a really bad day. Why would you possibly want a MutableObject?
 
Gerard Charles
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5796
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15274
38
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
Hibernate IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what is the last answer for question, Is it possible String mutable class.............?
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
Hibernate IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for welcoming me, I have read all post... but not got the answer...!!!
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Try http://algs4.cs.princeton.edu/12oop/MutableString.java.html, it looks like it's the same one.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic