I am a bit confused as to why an array length is a field and a String length is a function. does it mean that by using str.length() I traverse through all the chars of the String and return the count ?
Asked
Active
Viewed 58 times
1
-
You could check the source: [`String#length`](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/String.java#String.length%28%29) – Jorn Vernee Dec 07 '16 at 21:24
-
No you don't traverse. String keeps an array and it just returns its length. As to why string has a length method, it's because String is a class and not a primitive – denvercoder9 Dec 07 '16 at 21:24