Java中String类没有reverse方法?
为什么Java中的String
类没有reverse方法?相反,reverse()
方法是在 StringBuilder
中提供的?这是有原因的吗?但String有split()
、regionMatches()
等,它们比reverse()
方法更复杂。
当他们添加这些方法时,为什么不添加reverse()
?
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()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
既然 StringBuilder 中有它,那么 String 中就不需要它了,对吗? :-)
说真的,在设计 API 时,您可以包含很多东西。然而,为了简单和清晰起见,接口故意保持较小。 Google 搜索“API 设计”,您会发现大量 < a href="http://developer.qt.nokia.com/wiki/API_Design_Principles" rel="noreferrer"> 页面达成一致这。
如果您确实需要,可以按以下方法操作:
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:
理论上,String 可以提供它并仅将正确的结果作为新的 String 返回。当你认真思考时,这只是 Java 基础库的一个设计选择。
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.
如果你想要一个历史原因,字符串在Java中是不可变的,也就是说,如果不创建另一个字符串,则无法更改给定的字符串。
虽然这“本身”并不坏,但 Java 的初始版本缺少像 StringBuilder 这样的类。相反,String 本身包含(并且仍然包含)许多“更改”String 的方法,但由于 String 是不可变的,因此每个方法实际上都会创建并返回一个 NEW String 对象。
这导致了简单的表达式,例如:
在 ram 中实际创建 5 个字符串之类的东西,其中 4 个绝对无用,具有明显的性能问题(尽管对底层 char[] 数组进行了一些优化)。
为了解决这个问题,Sun 引入了 StringBuilder 和其他不可变的类。这些类可以自由修改单个 char[] 数组,因此调用方法不需要生成许多中间 String 实例。
他们最近添加了“reverse”,因此他们将其添加到 StringBuilder 而不是 String,因为这是现在操作字符串的首选方式。
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 :
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.
顺便说一句,在 Scala 中,您使用相同的 java.lang.String 类,并且确实获得了 reverse 方法(以及各种其他方便的东西)。它的实现方式是使用隐式转换,以便您的
String
自动转换为具有reverse
方法的类。它确实非常聪明,并且消除了使用数百个方法来膨胀基类的需要。As a side-note, in Scala you use the same
java.lang.String
class and you do get areverse
method (along with all kinds of other handy stuff). The way it does it is with implicit conversions, so that yourString
gets automatically converted into a class that does have areverse
method. It's really quite clever, and removes the need to bloat the base class with hundred of methods.字符串是不可变的,这意味着它不能更改。
当您反转字符串时,会发生的情况是每个字母都会自行切换,这意味着它每次都会创建新对象。
让我们看一下例子:
这意味着例如 Hello 变成如下
并且最终在堆上有 4 个新的 String 对象。
所以想一想,如果你有数千个或更多的字符串,那么将创建多少对象......这将是一个非常昂贵的。所以会占用太多的内存。
所以因为这个String类没有reverse()方法。
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
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.
嗯,我认为这可能是因为它是一个不可变的类,所以如果我们有一个反向方法,它实际上会创建一个新对象。
Well I think it could be because it is an
immutable
class so if we had a reverse method it would actually create a new object.verse() 作用于 this,修改当前对象,而 String 对象是不可变的——它们不能被修改。
原位执行reverse()特别有效 - 已知大小是相同的,因此不需要分配,循环迭代次数是副本中的一半,并且,对于大字符串,内存局部性是最佳的。从代码来看,我们花了很多心思来提高速度。我怀疑作者有一个需要高性能的特定用例。
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.