Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why string is not having Reverse method?

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all

As we know in String class there is no reverse method other than StringBuffer why they had not put Reverse method in String is there any technical reason behind it.if it let me know
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There definitely is.

String is immutable, meaning it can't be changed.

When you reverse a String, what's happening is that each letter is switched on it's own.

This means that for instance Hello becomes

elloh
lloeh
loleh
olleh

and you end up with 4 new String objects on the heap.

Now imagine doing that with a String of length 1000000 ... ;-)
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Sebastian Janisch

The only concern over here is creation of a new object for every operation on String.


Apart from this is there any other reason?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There might be, but the above is probably the most important one. It simply doesn't make any sense in the face of performance.
 
Rob Spoor
Sheriff
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastian Janisch wrote:This means that for instance Hello becomes

elloh
lloeh
loleh
olleh

and you end up with 4 new String objects on the heap.

Not necessarily. The string exists out of characters, so you can get those into a new char[], then do swaps inside that char[]. In code:
This requires a char[] besides the new String, so if the String contains 1.000.000 characters that means 3.000.000 characters in memory (original String, temporary char[], reversed String). However, if String would have a reverse method then it could skip the temporary char[]; String has a package private method (since at least Java 1.4) that can be used for this:
(the other String constructors that take a char[] make a copy of that char[])
This means that if String would have a reverse method, it could actually make the temporary char[] part of the reversed String, therefore requiring only 2.000.000 characters in memory. The same code as it would be when part of String:
However, this method does not exist, so using an intermediate StringBuffer or StringBuilder, or code like at the top of my post, is all that remains.
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

and you end up with 4 new String objects on the heap.

Sebastian, are you sure that is correct?

I thought the internal representation of the String and StringBuffer/StringBuilder was a char[] therefore you would just need to reverse the array and then create a new String from it.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The idea (as Sebastian Janisch) has already said is that String is for immutability and StringBuilder (usually better than StringBuffer) is for changing text. So many changing methods are moved into StringB...er.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic