How are arrays implemented in java?

心不动则不痛 提交于 2019-12-17 10:54:08

问题


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.


回答1:


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:

MySpecialCustomObject[] 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.




回答2:


We can say that An array is a container that holds a fixed length of data of single data type. eg.

int[] MyArray = new int[101]; // allocates memory for 101 integers, Range from 0 to 100.

and for multidimensional

String[][] names = {{"FirstName", "LastName"},{"Kaji", "Islam"},...};

and for character array

char[] ch={'a','b'....};


来源:https://stackoverflow.com/questions/2267790/how-are-arrays-implemented-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!