java中数组是如何实现的?
数组在java中是作为对象实现的,对吗?如果是这样,我在哪里可以查看数组类的源代码。我想知道数组中的长度变量是否被定义为常量,如果是的话,为什么它不是全部大写字母 LENGTH 以使代码更容易理解。
Arrays are implemented as objects in java right? If so, where could I look at the source code for the array class. I am wondering if the length variable in arrays is defined as a constant and if so why it isn't in all capital letters LENGTH to make the code more understandable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尽管数组在继承 java.lang.Object 的意义上是对象,但这些类是作为该语言的一个特殊功能动态创建的。它们没有在源代码中定义。
考虑这个数组:
没有这样的源代码。您已在代码中动态创建了它。
长度之所以是小写且是一个字段,实际上是因为在开发它时,后来的 Java 编码标准还不存在。如果今天开发一个数组,它可能是一个方法:getLength()。
长度是在对象构造时定义的最终字段,它不是常量,因此某些编码标准不希望其为大写。然而,一般来说,今天的 Java 中的所有内容通常要么作为大写常量完成,要么使用公共 getter 方法标记为私有,即使它是最终的。
Although arrays are Objects in the sense that they inherit java.lang.Object, the classes are created dynamically as a special feature of the language. They are not defined in source code.
Consider this array:
There is no such source code for that. You have created it in code dynamically.
The reason why length is in lower case and a field is really about the fact that the later Java coding standards didn't exist at the time this was developed. If an array was being developed today, it would probably be a method: getLength().
Length is a final field defined at object construction, it isn't a constant, so some coding standards would not want that to be in upper case. However in general in Java today everything is generally either done as a constant in upper case or marked private with a public getter method, even if it is final.
对于我们声明的每个数组,Java 中都有相应的类,但我们无法使用它。您可以使用 getClass().getName() 查看类
输出: [I
where "[ ”代表一维数组,“I”代表整数。
同样,我们可以有
等等。
For every Array we declare, corresponding classes are there in Java but it's not available to us.You can see the classes by using getClass().getName()
Output : [I
where "[" represents one dimension array and "I" represents Integer.
Similarly, we can have
and so on.
在Java中实现数组需要访问内存位置或进行指针运算。由于 Java 不允许您分配内存,因此它会为您执行数组实现。 Java 语言提供了这种实现。
Implementing array in Java requires access to memory location or do pointer arithmetic. As Java does not let you to allocate memory, it does the Arrays implementation for you. Java language provides that implementation.
我们可以说数组是保存固定长度的单一数据类型数据的容器。
例如。
对于多维
和字符数组
We can say that An array is a container that holds a fixed length of data of single data type.
eg.
and for multidimensional
and for character array