• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple question length instance variable for array object  RSS feed

 
Karen Haq
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm using Deitel and Deitel to learn Java. (Great book!) I'm on the chapter of Arrays. In the book, it states every array object knows its length and stores in a length instance variable. Isn't an "instance variable" the same thing as a "field"? I went to Java's SE's API to look at the properties of the Array Class and there is no field known as length, only a bunch of methods. And yes, I did see the method to retrieve the length of the array but, again, there is no "field" for length. How else would I have known Array objects have field variables? Are there other field variables??
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays in Java are kind of a special case - they have special rules defined for them that do not necessarily apply to either classes or primitives (though they're more like classes than primitives, except when they're not). Had Java been designed more consistently, in less of a rush, there probably would have been a JavaDoc page describing the length() method of an array. But it wasn't, and there isn't. Instead, we get custom rules in the JLS such as JLS 10.7, which addresses this particular issue.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I went to Java's SE's API to look at the properties of the Array Class

Unforunately, you are looking at the wrong place. The Array class is not the same thing as an array object. Your book refers to Java array objects, whose type is Object. Every array object has a final instance variable named "length".
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because arrays are, as Christophe said, objects, they also have the methods defined by java.lang.Object. These are not overridden so they maintain the default implementations of Object:
- hashCode returns the identity hash code that is also returned by System.identityHashCode
- equals returns whether or not the passed reference refers to the exact same object (i.e. it uses == only)
- clone returns a new array of the same type with the exact same contents. For object arrays these are references to the very same objects
- toString returns the class name (which has a special formatting for arrays) followed by @ and the hexadecimal hash code

java.util.Arrays has utility methods that return more useful values for hashCode, equals and toString if you need them.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob: um, what? That's all true, but how is any of that relevant to this question? Which, by the way, has already been answered correctly, and relevantly. Twice, for good measure. The question was about length. Which is neither a field nor method of Object. It's defined as a field for arrays, in the links that Christophe and I already gave.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:Rob: um, what? That's all true, but how is any of that relevant to this question?

I was just expanding on what Christophe said. I'd rather tell Karen now than have her come back and ask another question about why things don't work as she expects.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess that makes sense if you're addressing the "Are there other field variables?" part of the original post. Whereas Christophe and I were addressing the rest of the question. OK, thanks.
 
Karen Haq
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, gents!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!