Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array object vs. an array

 
Dan Walin
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Dan Walin
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great, thanks. I'm reading up more on the Class class. You've just enlightened me a lot!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic