• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why string is not having Reverse method?

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic