This week's book giveaways are in the iOS and Features new in Java 8 forums.
We're giving away four copies each of Barcodes with iOS: Bringing together the digital and physical worlds and Core Java for the Impatient and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes What is the difference between these two List intiializations? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the difference between these two List intiializations?" Watch "What is the difference between these two List intiializations?" New topic
Author

What is the difference between these two List intiializations?

Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 484

What is the difference between these two Lists?

and

I can use the .add method on each right?

I do not understand the difference between the two, or what kind of functionality I gain or lose when making the choice.
Can someone help me or direct me to some reading that will enlighten me?

Thanks


True wisdom is in knowing you know nothing - Socrates
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

The first one is an empty reference - a null reference if you will. So no, you can't call orderList.add(order); on it because you would get a NullPointerException, (unless you assign an object to it between the variable definition and method call).

The second one actually creates an Object of a suitable, concrete implementation of List, and so can have a method called on it without an error occurring.


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the difference between these two List intiializations?