How are arrays implemented in java?(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.
这篇关于java中的数组是如何实现的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:java中的数组是如何实现的?
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 获取数字的最后一位 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 转换 ldap 日期 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01