This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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 Java 8 in Action this week in the Java 8 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: 19556
    
  16

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: 10925
    
  12

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
Marshal

Joined: May 26, 2003
Posts: 29275
    
140

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 These 2 Declarations.
casting objects?
Help with Linked Lists
multithreading and concurrency
Difference between String variable and String Object ???