which should we prefer sample code :
List list = new ArrayList();
list.size()==0 or list.isEmpty()
with performance perspective.
which should we prefer sample code :
List list = new ArrayList();
list.size()==0 or list.isEmpty()
with performance perspective.
Assuming you are using an ArrayList
Frankly, it won't make much of a difference. Why?. isEmpty()
does this :
public boolean isEmpty() {
return size == 0;
}
Also, I think the JIT might inline this code so isEmpty()
might not take more time.
Do not try to tune performance on such a low level. It will not have any noticeale impact and might even result in the same bytecode depending on the compiler. Choose the more readable, self-expressive approach instead, which in this case is isEmpty
, as it says exactly what it does.