slavov
slavov

Reputation: 630

length field in array

As well known in java String class there is a length() method (why not getLength()?). If we look into this method we can see:

public int length() {
    return value.length;
}

and value is an array. Why do we access a field? I know that this field is final and nobody can change it, but still what about polymorphism? And where can I see that field? In which class? I've found java.lang.reflect.Array, but there is no such field as length.

Upvotes: 1

Views: 1401

Answers (5)

Adam
Adam

Reputation: 36703

I think you might be expecting Java to be a pure OO language... It's not... It's a mixture of Objects and primitives. Primitives do not behave like objects and have language level native stuff, like public length fields etc.

Consider the following... a character array... Its length field is NOT a field in the traditional sense, it is baked into the language at the JVM level. This difference means that you cannot use reflection to access it via Field.getDeclaredField(). Java provides a special mechanism for this case - java.lang.reflect.Array.getLength(Object)

Object obj = new char[] { 'a', 'b' };
int length = ((char[]) obj).length;  // .length looks like a field access but done by JVM
int lengthViaRelection = Array.getLength(obj);

This is very different from a standard field on a class where reflection can be used.

private static class ClassWithField {
    public int length;
}

ClassWithField obj = new ClassWithField();
int length = obj.length;  // .length is normal field access
int lengthViaRelection = (int) ClassWithField.class.getDeclaredField("length").get(obj);

Upvotes: 2

DaniEll
DaniEll

Reputation: 1040

String is a final class, so extending String is not possible / permitted and the compiler won't let you do so.

The Strings private member(!) value is an array of chars (the primitive). This can be considered an implementation detail and you should not mess with it.

All arrays in Java got a field named length. It is accessible via property-access, there is no need for something like a "getter".

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 121998

Where can i see that field? In which class?

If you are looking for char array declaration

That's a char array declared on top. You can see that.

The value is used for character storage.
112 
113     private final char value[];

And if you are looking for length field of an array

Array's are the part JVM implementation. You need to get the source code of JVM to analyze/see it.

And why we access a field?

That's how the length of an array can be known .

How about polymorthism? I know that this field is final and nobody can change it, but still what about polimorthis?

Never heard of it and if you mean Polymorphism, nothing to do here about it.

Upvotes: 3

mthmulders
mthmulders

Reputation: 9705

The length field of an array is not static; so each array will have its own field with its own value. The fact that it is readily has nothing to do with polymorphism; it's just a convenient way to find out information about the array.

The class java.lang.reflect.Array does not describe an array; it is a class that provides static methods to dynamically create and access Java arrays. So it is correct that there is no field length in that class.

Upvotes: 0

Smutje
Smutje

Reputation: 18123

value is an array of char (primitive, not java.lang.Character) and arrays of primitive types have a field named length:

final char[] chars = new char[1]; 
final int length = chars.length;

Upvotes: 1

Related Questions