Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.length() vs Array.length [duplicate]

Possible Duplicate:
Why is String.length() a method?
Java - Array's length property

Was there a specific design/performance reason as to why String has a method for length but Array has a variable length?

like image 586
Chander Shivdasani Avatar asked Aug 22 '12 22:08

Chander Shivdasani


1 Answers

There is no Array class in Java (other than in reflections). Arrays are "primitives" of a sort in Java and play by different rules from declared classes.

Certainly a length() method could have been defined on arrays, but the designers wanted to keep length as a property rather than a pseudo-method. (In part this may have made it easier for early Java implementations.) The reasons are somewhat buried in history.

(A better question is why Java couldn't decide whether to call the concept "length", "count", or "size" -- I always end up trying all three before I hit on the right one for an aggregating class.)

like image 174
Hot Licks Avatar answered Nov 08 '22 18:11

Hot Licks