Pradeep Kumar Pradeep Kumar - 5 months ago 9
Java Question

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()
?

Answer

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();