Why is list.size()>0 slower than list.isEmpty() in Java?

后端 未结 9 1937
误落风尘
误落风尘 2020-12-13 03:58

Why is list.size()>0 slower than list.isEmpty() in Java? On other words why isEmpty() is preferable over size()>0?<

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 04:07

    Your testing code is flawed.

    Just reverse the order, i.e call isEmpty first and size > 0 second and you'll get the opposite result. This is due to class loading, caching, etc.

提交回复
热议问题