• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Simple question length instance variable for array object

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Master Rancher
Posts: 4982
79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4982
79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4982
79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, gents!
 
Clowns were never meant to be THAT big! We must destroy it with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic