String.length()vs Array.length [英] String.length() vs Array.length
问题描述
是否有特定的设计/性能原因为什么String有一个长度方法但Array有一个可变长度?
Was there a specific design/performance reason as to why String has a method for length but Array has a variable length?
推荐答案
没有 Java中的数组
类(反射除外)。数组是Java中的一种原语,并且与声明的类有不同的规则。
There is no Array
class in Java (other than in reflections). Arrays are "primitives" of a sort in Java and play by different rules from declared classes.
当然是 length()
方法可以在数组上定义,但设计者希望将 length
保留为属性而不是伪方法。 (在某种程度上,这可能使早期Java实现变得更容易。)原因在历史上有些埋没。
Certainly a length()
method could have been defined on arrays, but the designers wanted to keep length
as a property rather than a pseudo-method. (In part this may have made it easier for early Java implementations.) The reasons are somewhat buried in history.
(一个更好的问题是为什么Java无法决定是否称之为长度,计数或大小这个概念 - 我总是在尝试所有这三个之前尝试所有这三个,然后才能找到合适的聚类。)
(A better question is why Java couldn't decide whether to call the concept "length", "count", or "size" -- I always end up trying all three before I hit on the right one for an aggregating class.)
这篇关于String.length()vs Array.length的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!