It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Why string is not having Reverse method? 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 » Beginning Java
Bookmark "Why string is not having Reverse method?" Watch "Why string is not having Reverse method?" New topic
Author

Why string is not having Reverse method?

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
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


Creativity is nothing but Breaking Rules
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
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 ... ;-)


JDBCSupport - An easy to use, light-weight JDBC framework -
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
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

Joined: Feb 23, 2009
Posts: 1183
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

Joined: Oct 27, 2005
Posts: 19674
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96

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

Joined: Oct 13, 2005
Posts: 38449
    
  23
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Why string is not having Reverse method?