aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes length variable of arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "length variable of arrays" Watch "length variable of arrays" New topic
Author

length variable of arrays

Lalit Bansal
Ranch Hand

Joined: Jan 29, 2007
Posts: 63
Hi,

I want to know in which class is the "length" variable defined.

Thanks in advance.
Kai Witte
Ranch Hand

Joined: Jul 17, 2004
Posts: 356
hello,

it is defined in every array type. Arrays do not have a common superclass which defines it. See JLS 10.7

Kai


Kai Witte's business website Kai Witte's private homepage
Lalit Bansal
Ranch Hand

Joined: Jan 29, 2007
Posts: 63
Thanks for the response.

Actually, I assume that there must be some class where this public variable is defined.
Can you please let me know which is that class?
Andy Morris
Ranch Hand

Joined: May 30, 2004
Posts: 78
arrays are a special type of object that you wont be able to find the API and see a public 'length' attribute. Wrt the exam, I wouldn't see the point probing much further into this area.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Lalit Bansal:
...Actually, I assume that there must be some class where this public variable is defined...

It seems logical to expect a java.lang.Array class in the API, but instead arrays are built into the language itself. The details are documented in Chapter 10 of the Java Language Specification (JLS). In particular, the "length" member is described under 10.7 - Array Members.
[ March 08, 2007: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Lalit Bansal
Ranch Hand

Joined: Jan 29, 2007
Posts: 63
Thanking all of you for clarifying on same.

The JLS mentions:
An array thus has the same public fields and methods as the following class:

class A implements Cloneable, java.io.Serializable {
public final int length = X;
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError(e.getMessage());
}
}
}


Hence, I assume that whenever we declare and initialize an array, it becomes an instance of this class (i.e., class A).

Please correct me if my assumption is wrong.

Thanks.

NB: I know that such in-depth details are not required for the SCJP exam. It was only because of my curiousity I asked the same. Please excuse me for mentioning this problem on wrong forum.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: length variable of arrays