6

In which class is the length field defined in Java (e.g. for array length)? Will I be able to see it defined say in Object class?

EDIT : Why was this field so designed(any thing related with security or memory efficiency)?

polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
abson
  • 9,148
  • 17
  • 50
  • 69

7 Answers7

12

Array types are special in Java. This is an excerpt from JLS 10.7 Array Members

The members of an array type are all of the following:

  • The public final field length, which contains the number of components of the array (length may be positive or zero).
  • The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions. The return type of the clone method of an array type T[] is T[].
  • All the members inherited from class Object; the only method of Object that is not inherited is its clone method.

Despite this, there are still old-standing bugs when you're using reflection on arrays: neither length nor clone could be found through reflection (bug# 5047859, bug# 4987375).

Neither member is inherited in the traditional way from any superclass; all array types extends from Object directly. This "special treatment" is likely why these bugs exist in the first place.


"does this mean that never will one be able to see the length variable being defined?"

There is no actual source code for array types. Again, these types are special; the JVM just pulls out these types out of a hat whenever they're required. You will not see a .java source file for the array type int[].class (i.e. the type of all int array).

So, no, you will not be able to see the length field defined.


For further reading, here are some information on Java Reflection:

polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • does this mean that never will one be able to see the length variable being defined? – abson Mar 23 '10 at 06:18
  • i didn't understand 'could be found through reflection' phrase here could you please explain it? – abson Mar 23 '10 at 06:44
  • @abson: I added some remarks to address your comments. Feel free to ask for more clarification. – polygenelubricants Mar 24 '10 at 07:05
  • @polygenelubricants could you please tell me the reason for such a special treatment for length field. – abson Mar 25 '10 at 07:06
  • 1
    The special treatment is actually on array types as a whole. You can define arrays of any type of any dimension. `int[].class` is an array type, `int[][].class` is another array type, so is `Thing[].class` and `Stuff[][].class`. Java has to create these types on-demand as required; there is no source code for them. – polygenelubricants Mar 25 '10 at 07:18
  • @polygenelubricants thanks for your promptness if you don't mind could suggest a java book that deals with basics for me – abson Mar 25 '10 at 07:24
  • I never studied Java from basics, so I don't know a good book for that. Eventually, however, you **MUST** read Josh Bloch's _Effective Java_. – polygenelubricants Mar 25 '10 at 07:27
5

Arrays are not like regular classes, they are a special case defined in the Java Language Specification. So the .length public final variable is defined in the Java Language Specification, but it is not actually defined anywhere in a .java or a .class file.

Yishai
  • 90,445
  • 31
  • 189
  • 263
  • does this mean that it is defined in Object class? – abson Mar 22 '10 at 15:47
  • +1 for beating me to the JLS link. @abson, no, it is not in the Object class. If you print out someArray.getclass(), you get something non-human-readable, it's a special type built into the language. – Pops Mar 22 '10 at 15:50
2

You need to be a bit more specific than that.

A variable length can exist in any class. If you mean from the API then you are probably looking for the Array.

Some classes have a length() method.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
2

In java with arrays you can do this. For example:

String[] a = new String [] {"a","b","c"};
int length = a.length;

Length would be 3;

Jacob Schoen
  • 14,034
  • 15
  • 82
  • 102
  • but he is asking where is the length var? – mohdajami Mar 22 '10 at 15:38
  • yes @jschoen could you tell me as to where length variable used in your program is defined? – abson Mar 22 '10 at 15:50
  • 1
    As Yishai stated, it is not really defined in the way you are thinking. It is part of the language itself. Built in, if you will. So you will not find it in .java file. Maybe you are asking the wrong question. What is it you are trying to accomplish by tracking this down? Maybe we could be more help with that. – Jacob Schoen Mar 22 '10 at 16:09
  • I didn't get this 'part of the language itself' line. Could @jschoen explain as to what this so called JLS is? – abson Mar 22 '10 at 16:22
  • 1
    The JLS is the Java Language Specification. It is long, and honestly not a fun read, but is interesting. It essentially is a document explaining the details of the Java language, the standards, etc. It is defined should be used as a resource for you programming java. – Jacob Schoen Mar 22 '10 at 23:41
1

Any class can have a field called length. Just add it.

Arrays do not have a length field; they have something which looks like such a field:

int[] a = new int[10];
System.out.println("array length is: " + a.length);

but it is not really a plain field, because this does not compile:

int[] a = new int[10];
a.length = 42;  // <- here the compiler gets grumpy

and, at the JVM level, the array length is accessed with a specific opcode (arraylength) and not the generic instance field access opcode (getfield).

Thomas Pornin
  • 72,986
  • 14
  • 147
  • 189
0

Its a variable that can be accessed for an array to find out how many elements there are in the array.

0

There is no single answer to this question. Anyone can include a length variable in a class, but there's no guarantee that it exists -- or, if it does exist, that it's public -- in any given class. You really just have to read the source for the class you're interested in.

I will say that in the widely-used JavaBean convention, classes that could reasonably have a length attribute but don't have a public variable length often have getLength() and setLength() methods. That's better than a public variable for a number of reasons that are kind of out of scope for your question, but if you're interested, check out this related SO question.

Community
  • 1
  • 1
Pops
  • 30,199
  • 37
  • 136
  • 151
  • Yes its the length property of arrays could you please tell where it actually is defined? – abson Mar 22 '10 at 15:44