Thursday, May 25, 2006

Difference between length variable and length() method

For String you use "length()", for an array you use just "length".

The reasoning is that String is just a class. You could make one similar yourself if you want. Therefore operations on it should be methods like "length()". To work out the length of a String it might be necessary to go through and count all the characters.

An array is really a built-in data structure. The nember of slots in an array is set when it is created, and can never change without creating a whole new array, so it is reasonable to put that value in a member variable.

NOTE:

length is a final member of all arrays.



0 Comments:

Post a Comment

<< Home