StringBuilder length() 方法的时间复杂度

Time complexity of StringBuilder length() method

StringBuilder 的 length() 方法的时间复杂度是多少 class?

String str = "hello";
StringBuilder sb = new StringBuilder(str);

System.out.println(sb.length()); // should print 5

我的假设是 O(n),其中 n 是字符串中的字符数,类似于 String class.

中的 length()

这是StringBuilder中的一个成员变量 class:

/**
 * The count is the number of characters used.
 */
int count;

这是length()的代码:

/**
 * Returns the length (character count).
 *
 * @return  the length of the sequence of characters currently
 *          represented by this object
 */
@Override
public int length() {
    return count;
}

你怎么看?