What is complexity of length() function in String class of Java?

后端 未结 4 1778
栀梦
栀梦 2021-02-20 07:53

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

4条回答
  •  故里飘歌
    2021-02-20 08:33

    The complexity is O(1) Since String class have the length as a field. It's not O(n^2).

提交回复
热议问题