While reading about Java I think I understand that there is something called an Array class which has methods and properties and then there is an array which is part of the Java language. I was taking a practice test and then looking at the explanation of the answers where they said an array has a "property" of "length". I thought only an object can have properties. Can anyone help me understand the true nature of an array in Java? Example: why can I create an array with "int mynum" and not have to use "Array mynum = new Array()". I may be way off in my understanding so if someone can point me to a good place to read up on this, that would help too. Thanks.
An array is an Object. Any Java context that accepts Object as a parameter will accept an array. The Array class is a utility class for creating and accessing java arrays. It is not the same as the classes that define a arrays in java. The API docs for the Class class state: Instances of the class Class represent classes and interfaces in a running Java application. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions.
That is why arrays can and do have a length property. It would seem obvious that by using the new operator that arrays must be objects for example:
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Joined: Nov 11, 2003
Great, thanks. I'm reading up more on the Class class. You've just enlightened me a lot!