Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: why Array.length has no () but String.length() does? [duplicate]

Tags:

java

Why when we get the length of an array don't we have to place () like

int [] ar;
System.out.println(ar.length); // no parentheses 

but for a String it will be

String st;
System.out.println(st.length());
like image 644
C graphics Avatar asked Jul 03 '14 22:07

C graphics


People also ask

Why is there an array length without parentheses?

The reason is, that an array is a language construct - Therefore, whenever you want to know something about the array (it's length or else) You have to retrieve a fixed value from the JVM, rather than calling a method that evaluates something.

What is the difference between string length () and array length?

Array Length field cannot be accessed as any normal field. String class objects are immutable and so we cannot use the final field with the String objects. In the case of String, class accessor methods have to be used by the class objects.

Why is Java array length not a method?

Since arrays are fixed length defined at the time they are instantiated length is a public final field on the class. There is no need to make it a method since there is no calculation to be done at run time.

Is length () a string method in Java?

length() method. The Java String length() method is a method that is applicable for string objects. length() method returns the number of characters present in the string. The length() method is suitable for string objects but not for arrays.


3 Answers

why Array.length has no () but String.length() does?

At the syntactic level, it really comes down to "historical reasons" and "because that's the Java (pre) 1.0 language designers decided to do it". The only people who can truly tell you why are the language designers themselves.

However:

  • The length field is special in the sense that it is actually in the array object's header, and fetching the length of an array uses a special bytecode.

  • The length field cannot be changed by any means short of dangerous native code hacks. It is really important that this be so, to avoid problems with heap corruption, etc. (By contrast even a final field on a normal object can be modified using reflection.)

In these respects, length on an array is genuinely different from any other field.


Since the early days of (pre-)Java, the use of exposed fields in APIs has gone right out of style.

Most people would say that this is a good thing. By exposing the length information as length() and size() methods on strings, collections, etc, the class library design allows a Java implementor to change the representation of the respective types. That is not possible (in Java) if fields are exposed directly.

like image 178
Stephen C Avatar answered Oct 07 '22 13:10

Stephen C


The difference is:

  • The length of an array is NOT about the actual content - it's the dimension! An array can be of length 10, but only contain 2 elements.
  • An Object containing elements (A String is an Object, containing multiple chars) might have a certain count of Objects inside. This count can vary, depending on whether you add or remove elements. So the length() method is about the actual content!

The reason is, that an array is a language construct - Therefore, whenever you want to know something about the array (it's length or else) You have to retrieve a fixed value from the JVM, rather than calling a method that evaluates something.

(The Array-Class is just a proxy for the actual array-construct.)


Technically spoken an array (note the small a) is just a memory allocation for array.length elements in the memory. (And that's why you can't simple resize an array, but always need to create a NEW array when changing the dimension - it needs to be relocated (in C that was called realloc) in memory, so there is enough room for array.length * elementSize bits.

A List on the other hand does not care about the actual position of it's element in the memory, and therefore allows dynamic resizing at any time. (So it can server a method size() because it has no dimension, and just needs to return information about it's content.)

like image 31
dognose Avatar answered Oct 07 '22 11:10

dognose


This is the difference between accessing a field (no parentheses) and calling a method (requires parentheses, even if there are no arguments to the method).

Historically, arrays could have had a method in addition to or instead of a field. Strings could have had a field in addition to a method. A field is simpler, which is probably why arrays use it, but there are at least two reasons why it's good for String to have a length method:

  1. String implements the CharSequence interface, which defines a length() method, so String has to have that method. (Defining fields isn't a feature of interfaces, except for static final constants.) String could have length as a method and a field but that would be distracting.

  2. Flexibility. String's internal implementation has changed over time: at one time it used an internal char array and separate offset and length fields which defined a region within the char array. That was done so that substrings could share the parent string's char array (with different offset and length) instead of copying the array. It was later found that that optimization was rarely beneficial, so now, String has only a char array of the correct size, and the length method reads the array's length field. String no longer has its own length field, not even a private one, but they couldn't have removed it if it were part of the class's public API, which would give less flexibility to experiment with internal implementations.

like image 37
Boann Avatar answered Oct 07 '22 12:10

Boann