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.
-
4If it was a constant (as in a static final field), all arrays would have to have the same size (or arrays of different sizes would have to have different types (which would further imply that an array's size would have to be known at compile-time)). – sepp2k Feb 15 '10 at 17:47
-
From what I am understanding from Yishai's answer below is that the array is dynamically created when it is defined. – AFK Feb 15 '10 at 17:52
-
sepp2k's point is that there is no one global length field. Generally we think of upper case variables as public static and final. Here length is public and final, but not static, and it's value is different per object instance, although it never changes. – Yishai Feb 15 '10 at 18:29
-
Useful - http://stackoverflow.com/questions/3965458/source-code-for-java-array?rq=1 – Raúl Apr 10 '15 at 15:25
4 Answers
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,555
- 3
- 21
- 29

- 90,445
- 31
- 189
- 263
-
-
2@sn3twork, Basically array has all of the methods of object, plus a public final length variable and it has a public clone method, implements Cloneable and Serializable (but I assume for legacy reasons not Iterable). Details are in the JLS: http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html – Yishai Feb 15 '10 at 17:55
-
thanks for the link http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html#11358 I found where it talks about defining arrays using arrays initializers and creation expressions – AFK Feb 15 '10 at 18:02
-
-
1The above link no longer contains the specified information. The updated link is https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.3 – shivamag00 Oct 10 '20 at 10:28
For every Array we declare, corresponding classes are there in Java but it's not available to us.You can see the classes by using getClass().getName()
int[] arr=new int[10];
System.out.println(arr.getClass().getName());
Output : [I
where "[" represents one dimension array and "I" represents Integer. Similarly, we can have
[F for one-dimensional float arrays
[Z for one-dimensional boolean arrays
[J for one-dimensional long arrays
[[I for two-dimensional int arrays
and so on.

- 41
- 4
Implementing array in Java requires access to memory location or do pointer arithmetic. As Java does not let you to allocate memory, it does the Arrays implementation for you. Java language provides that implementation.

- 9
- 1
- 3
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'....};

- 29
- 4