Difference between size and length methods?

前端 未结 6 655
北海茫月
北海茫月 2020-12-12 14:46

What is the difference between .size() and .length ? Is .size() only for arraylists and .length only for arrays?

6条回答
  •  没有蜡笔的小新
    2020-12-12 15:05

    length variable:
    

    In Java, array (not java.util.Array) is a predefined class in the language itself. To find the elements of an array, designers used length variable (length is a field member in the predefined class). They must have given length() itself to have uniformity in Java; but did not. The reason is by performance, executing length variable is speedier than calling the method length(). It is like comparing two strings with == and equals(). equals() is a method call which takes more time than executing == operator.

    size() method:
    

    It is used to find the number of elements present in collection classes. It is defined in java.util.Collection interface.

提交回复
热议问题