File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The difference between null and isEmpty.

 
Mohammad Ashrafuddin Ferdousi
Ranch Hand
Posts: 111
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI all,

Can anyone please clearify me that what is the difference between:


!mylist.isEmpty() and mylist!=null

where mylist can be an object of ArrayList.

Is they produce the same result? Where i can apply the 1st one and when i should apply the 2nd one?

please help.

Regards
 
pete stein
Bartender
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One tests if the ArrayList variable is null, the other if the ArrayList object holds any items in its list. You would test for null first, and then if not null, check if its empty or not. The reason to do it in this order is if you test if its empty and it happens to be null, you'll throw a NullPointerException for trying to call a method on a null object.
 
Rob Spoor
Sheriff
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An empty (Array)List is still an (Array)List, null isn't. Plain and simple.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One tests to see if you have an egg carton. The other tests to see if there are any eggs in your egg carton.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33680
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred: That is a great analogy!
 
Mohammad Ashrafuddin Ferdousi
Ranch Hand
Posts: 111
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you all for your support. have a nice day, friend.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic