简体   繁体   中英

No reverse method in String class in Java?

Why there is no reverse method in String class in Java? Instead, the reverse() method is provided in StringBuilder ? Is there a reason for this? But String has split() , regionMatches() , etc., which are more complex than the reverse() method.

When they added these methods, why not add reverse() ?

Since you have it in StringBuilder, there's no need for it in String, right? :-)

Seriously, when designing an API there's lots of things you could include. The interfaces are however intentionally kept small for simplicity and clarity. Google on "API design" and you'll find tons of pages agreeing on this.

Here's how you do it if you actually need it:

str = new StringBuilder(str).reverse().toString();

If you want an historical reason, String are immutable in Java, that is you cannot change a given String if not creating another String.

While this is not bad "per se", initial versions of Java missed classes like StringBuilder. Instead, String itself contained (and still contains) a lot of methods to "alter" the String but since String is immutable, each of these methods actually creates and return a NEW String object.

This caused simple expressions like :

String s = "a" + anotherString.substr(10,5).trim().toLowerCase();

To actually create in ram something like 5 strings, 4 of which are absolutely useless, with obvious performance problems (despite after there has been some optimizations regarding underlying char[] arrays).

To solve this, Sun introduced StringBuilder and other classes that ARE NOT immutable. These classes freely modify a single char[] array, so that calling methods does not need to produce many intermediate String instances.

They added "reverse" quite lately, so they added it to StringBuilder instead of String, cause that's now the preferred way to manipulate strings.

Theoretically, String could offer it and just return the correct result as a new String. It's just a design choice, when you get down to it, on the part of the Java base libraries.

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, means it will always create the new object each times.

Let us see with example: This means that for instance Hello becomes as below

  • elloH lloeH loleH olleH

and you end up with 4 new String objects on the heap. So think if you have thousands latter of string or more then how much object will be created.... it will be really a very expensive. So too much memory will be occupied.

So because of this String class not having reverse() method.

嗯,我认为这可能是因为它是一个immutable类,所以如果我们有一个反向方法,它实际上会创建一个新对象。

As a side-note, in Scala you use the same java.lang.String class and you do get a reverse method (along with all kinds of other handy stuff). The way it does it is with implicit conversions , so that your String gets automatically converted into a class that does have a reverse method. It's really quite clever, and removes the need to bloat the base class with hundred of methods.

reverse() acts on this , modifying the current object, and String objects are immutable - they can't be modified.

It's peculiarly efficient to do reverse() in situ - the size is known to be the same, so no allocation is necessary, there are half as many loop iterations as there would be in a copy, and, for large strings, memory locality is optimal. From looking at the code, one can see that a lot of care was taken to make it fast. I suspect the author(s) had a particular use case in mind that demanded high performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM