2 ms·
Array is a fixed size memory, therefore it’s unnecessary to invoke a method A string has a length, because it counts the number of characters, which can have
by jbverschoor 3y ago
Array is a fixed size memory, therefore it’s unnecessary to invoke a method
A string has a length, because it counts the number of characters, which can have different sizes.
Both arrays and strings represent a 1-dimensional string or away of things.
Collections have sizes, because collections are more generic. Would a binary tree have a length? Maybe.. but it’s ambiguous and in most cases not correct
- TOGoS 3y agoUh-huh. And yet they can (according to the article) turn .length into a special opcode, even though it looks like any other property access. So the Java compiler could just as easily see size() being called on what it knows is an array and turn that into a special opcode. We could argue about the semantic differences between length-as-in-number-of-characters and size-as-in-bytes, but for most purposes, i.e. when any of String, char[], or List<Character> would be logically equivalent, there's no technical reason for having different APIs for what are essentially different implementations of the same thing. Like when Rasmus couldn't stick to a naming convention for PHP functions, and it turned out to be because he couldn't be bothered to write a decent hash function. So everyone who uses the language from now to forever has to memorize this inconsistent naming scheme because the language designers couldn't get their shit together. Yeah, I get it; sometimes it's hard to foresee these things from the beginning. But it annoys me when people make up excuses for these inconsistencies that don't hold water instead of just admitting that someone messed up.