数组是如何在java中实现的? [英] How are arrays implemented in java?

查看:29
本文介绍了数组是如何在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.

长度和字段的原因是小写的,实际上是因为在开发该字段时不存在后来的 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().

Length 是在对象构造时定义的最终字段,它不是常量,因此一些编码标准不希望它是大写的.然而,在今天的 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中实现的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆