Java: why Array.length has no () but String.length() does?
java
Solution
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.
Problem
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()); ```