Is it O(n) or O(1) (By saving length in a private variable during string allocation to the object).
if it is O(n), does it mean that the complexity of following code is
The complexity is O(1) Since String class have the length as a field. It's not O(n^2).
O(1)
String
field