wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes The difference between null and isEmpty. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The difference between null and isEmpty." Watch "The difference between null and isEmpty." New topic

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.

pete stein

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

Joined: Oct 27, 2005
Posts: 19651

An empty (Array)List is still an (Array)List, null isn't. Plain and simple.

How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11153

One tests to see if you have an egg carton. The other tests to see if there are any eggs in your egg carton.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeanne Boyarsky
internet detective

Joined: May 26, 2003
Posts: 30076

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.
I agree. Here's the link: http://aspose.com/file-tools
subject: The difference between null and isEmpty.
Similar Threads
Difference between String variable and String Object ???
Difference Between These 2 Declarations.
multithreading and concurrency
Help with Linked Lists
casting objects?