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中的数组是如何实现的?
基础教程推荐
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01