-6

which should we prefer sample code :

 List list = new ArrayList();
    list.size()==0 or list.isEmpty()

with performance perspective.

Avyaan
  • 1,285
  • 6
  • 20
  • 47
  • 1
    So, you've profiled your code and you've found that this method call is a true bottle-neck in your code's performance? If not, this sounds like spinning wheels and premature optimization. – Hovercraft Full Of Eels Jun 02 '15 at 11:37
  • no.it was not like that. it was just for better understanding.and thanx whoever provided me the link for the answer. – Avyaan Jun 02 '15 at 11:41
  • @abhinaba yes i know that i know nothing but i know how to take a look over java api. it's k..thanx . – Avyaan Jun 02 '15 at 11:45

2 Answers2

2

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.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
2

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.

LionC
  • 3,106
  • 1
  • 22
  • 31
  • I don't think that we could ever get the *same* byte code. I mean the methods are part of the `List` interface. They are overridden.. The compiler just can't *place* the method there – TheLostMind Jun 02 '15 at 11:45