| Author |
The difference between null and isEmpty.
|
Mohammad Ashrafuddin Ferdousi
Ranch Hand
Joined: Jan 14, 2010
Posts: 111
|
|
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
Joined: Feb 23, 2007
Posts: 1561
|
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
|
An empty (Array)List is still an (Array)List, null isn't. Plain and simple.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9947
|
|
|
One tests to see if you have an egg carton. The other tests to see if there are any eggs in your egg carton.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26184
|
|
|
Fred: That is a great analogy!
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
Mohammad Ashrafuddin Ferdousi
Ranch Hand
Joined: Jan 14, 2010
Posts: 111
|
|
thank you all for your support. have a nice day, friend.
|
 |
 |
|
|
subject: The difference between null and isEmpty.
|
|
|