Is ArrayList.size() method cached?

后端 未结 7 1873
再見小時候
再見小時候 2020-12-31 02:58

I was wondering, is the size() method that you can call on a existing ArrayList cached? Or is it preferable in performance critical code t

7条回答
  •  没有蜡笔的小新
    2020-12-31 03:28

    I don't think I'd say it's "cached" as such - but it's just stored in a field, so it's fast enough to call frequently.

    The Sun JDK implementation of size() is just:

    public int size() {
        return size;
    }
    

提交回复
热议问题