Possible Duplicate:
In Java, for a string x, what is the runtime cost of s.length()? Is it O(1) or O(n)?
How exactly does the length method of String class work in Java ?
Is it a for loop in linear time or there is a field keeping the track of length so that its constant time ?
The length of a
Stringis kept as a field as seen here : http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/String.java#String.length%28%29