aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The difference between null and isEmpty." Watch "The difference between null and isEmpty." New topic
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: 19722
    
  20

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

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
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

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.