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.
推荐答案
虽然数组在继承 java.lang.Object 的意义上是对象,但类是作为语言的一个特殊特性动态创建的.它们没有在源代码中定义.
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.
考虑这个数组:
MySpecialCustomObject[] array;
没有这样的源代码.您已经在代码中动态创建了它.
There is no such source code for that. You have created it in code dynamically.
length 和 field 之所以是小写的,实际上是因为后来的 Java 编码标准在开发时并不存在.如果今天正在开发一个数组,它可能是一个方法:getLength().
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().
长度是在对象构造时定义的最终字段,它不是一个常数,所以一些编码标准不希望它是大写的.但总的来说,在今天的 Java 中,所有内容通常要么作为大写常量完成,要么使用公共 getter 方法标记为私有,即使它是最终的.
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.
相关文章